How to delete elements from structure
751 views (last 30 days)
Show older comments

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
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.
Accepted Answer
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
More Answers (2)
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
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.
Laxmikant Sharma
on 13 Jul 2022 at 10:22
Edited: Laxmikant Sharma
on 13 Jul 2022 at 11:22
You can completely skip the index of the element you want to delete:
Say
s
is a struct of
size: 1x5
And you have to delete the 2nd element of it.
Performing...
s(2) = [];
will leave the size unchanged, so... perform the below action (generalized for a; for above example a = 2):
s = s([1:a-1, a+1:end]); %DO THIS
Now...the updated ize of s will be
size: 1x4
1 Comment
Jan
on 13 Jul 2022 at 11:14
I'd insert a comma for clarity:
s = s([1:a - 1, a + 1:end])
But this looks easier:
s(a) = [];
See Also
Categories
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!