How to delete elements from structure

782 views (last 30 days)
Ashok Das
Ashok Das on 22 Jan 2019
Commented: Ashok Das on 23 Jan 2019
I have a structure of the above kind.
Lets choose , cell.population(1).type = 1
cell.population(2).type = 2
cell.population(1).profile = [1 1 2]
cell.population(2).profile = [2 1 2]
Now if I use use " cell.population(1).profile = [ ] " and " cell.population(1).type = [ ] " to delete the profile and type, then it doesn't get deleted. It remains as a blank vector ( [ ] ) in memory.
What should I do?
  2 Comments
Stephen23
Stephen23 on 23 Jan 2019
"Now if I use use " cell.population(1).profile = [ ] " and " cell.population(1).type = [ ] " to delete the profile and type, then it doesn't get deleted. It remains as a blank vector ( [ ] ) in memory."
That is the expected behavior: just like any other container variables, each field must contain something, even if it only an empty array. Judging by your comments there seems to be some confusion about structure arrays. It is important to know that
  • the number of elements (i.e. the size of the structure array) and
  • the number of fields (which are the same for all structure elements)
are totally independent of each other. This means:
  • You can remove a field, and that field will be removed from all elements of the array.
  • You can remove element/s of the structure, so that the structure will have fewer elements (and a different size). This is exactly like every other array in MATLAB.
"What should I do?"
We don't know because it is not clear what you expect to achieve (you did not explain this). In any case, you could either remove element/s of the structure (like I showed you) or remove field/s from all elements of the structure (as Jan showed you): which do you want to do?
An alternative is to use a cell array of scalar structures, but this is less efficient and less convenient to work with.
Do NOT use cell as a variable name.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 22 Jan 2019
Edited: Stephen23 on 22 Jan 2019
Deleting an element of a structure is easy, and it has nothing to do with fields:
S.population(1).type = 1;
S.population(2).type = 2;
S.population(1).profile = [1 1 2];
S.population(2).profile = [2 1 2];
S.population(1) = [] % delete the first element of S.population
  3 Comments
Ashok Das
Ashok Das on 23 Jan 2019
This is working. Thanks.

Sign in to comment.

More Answers (1)

Jan
Jan on 22 Jan 2019
Edited: Jan on 22 Jan 2019
If you want to delete a field, use rmfield:
cell.population = rmfield(cell.population, 'type')
Of course, this removes the field from all elements of the array cell.population(:). You cannot remove a field from one element of a struct array only.
If this is time-critical, you can use the faster C-Mex function: FileExchange: fRMField
  2 Comments
Jan
Jan on 22 Jan 2019
If yout delete the "first value", the field is still existing and contains the empty matrix. See this:
clear S % Just to be save
S.Field(2).SubField = 23;
S.Field(1)
You see, that the SubField exists in the Field(1) already, although it way not initialized yet.
This is the nature of struct arrays: All elements have the same fields.
If you want an array, which contains structs with different fields, you need a cell array, whose elements are structs. This is less efficient, but more flexible.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by