How to delete elements from structure
751 views (last 30 days)
Ashok Das on 22 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?
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
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:
is a struct of
And you have to delete the 2nd element of it.
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
Thanks @Jan to make it clearer, edited accordingly.
Now...the updated ize of s will be