2

For simplicity's sake let us stick to this official Matlab example.

I run the following code, and a structure with three fields is created.

patient.name = 'John Doe';
patient.billing = 127.00;
patient.test = [79, 75, 73; 180, 178, 177.5; 220, 210, 205];

Then, in order to create a structure array, I simply have to do the following.

patient(2).name = 'Ann Lane';
patient(2).billing = 28.50;
patient(2).test = [68, 70, 68; 118, 118, 119; 172, 170, 169];

At this point I have a structure array that is clearly represented by this picture: enter image description here

Here comes the question: how can I empty all the fields of the structure patient(2)?

Ideally (forgive the abuse of notation) I would like to do something like patient(2).* = {}.

Calling the fields one by one is not an option because in my actual code I have a lot of fields, neither I can convert the structure to a cell.

Thanks in advance!

1
  • 1
    @Ander Biguri Thank you so much for adding the image to the question, I did not have the necessary reputation at the time I posted :) Commented Jul 18, 2014 at 15:25

4 Answers 4

3

If this operation is frequent, a good idea would be to prepare an EMPTY_PATIENT:

EMPTY_PATIENT = struct( ...
   'name', {},          ...
   'billing', {},       ...
   'test', {}           ...
);

then use the simple assignment (which is very fast) to empty the position:

patient(2) = EMPTY_PATIENT;
Sign up to request clarification or add additional context in comments.

2 Comments

For sure this is the safest and fastest way to go, using a foo empty structure! Thank you CST :)
As a general remark: if there is only one level and you have a lot of fields to populate, you can get away with entering one fieldname and MATLAB will populate the remainder. For example: patient(end+1).name = []; empty_patient = patient(end); patient(end) = []; Saves some typing
1

I'm sure there's a clever way to do it without the loop but this works fine.

fieldstoclear = fieldnames(patient(2));

for ii = 1:length(fieldstoclear)
    patient(2).(fieldstoclear{ii}) = [];
end

edit: forgot about structfun:

patient(2) = structfun(@(x) [], patient(2), 'UniformOutput',false)

Note: Neither method will preserve nested structures.

1 Comment

Thank you excaza, this goes in the right direction. Anyway the nested structures are of importance in this case, so I cannot really accept this as an answer! But again it really is a step forward, thank you!
0
 patient(2) = [];

Might be what you are looking for. This will eliminate that element from the structure.

1 Comment

Thank you for your answer, but I actually need to keep the element because the size of the array plays a role in my code. I expressly need to empty its field, not to delete it.
0

This should be a generic solution for any number of fields.

fields=fieldnames(patient);
patient(2)=cell2struct(cell(length(fields), 1), fields);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.