How to delete elements from structure

525 Ansichten (letzte 30 Tage)
Ashok Das
Ashok Das am 22 Jan. 2019
Bearbeitet: Stephen23 am 26 Dez. 2022
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 Kommentare
Jan
Jan am 22 Jan. 2019
By the way, "cell" is a bad choice for a variable, because this shadows the important Matlab function cell().
Stephen23
Stephen23 am 23 Jan. 2019
Bearbeitet: Stephen23 am 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 22 Jan. 2019
Bearbeitet: Stephen23 am 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 Kommentare
Jan
Jan am 22 Jan. 2019
After S.population(1) = [], the array S.population has lost an element:
S.population(1).type = 1;
S.population(2).type = 2;
S.population(1).profile = [1 1 2];
S.population(2).profile = [2 1 2];
size(S.population)
S.population(1) = []
size(S.population)
This means, that removing the element of the struct array does not "occupy the place". This is only the fact for the fields. You can either remove a field in the complete cell array, set the value of a field to [] or remove an element of the struct array.
Ashok Das
Ashok Das am 23 Jan. 2019
This is working. Thanks.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (3)

Jan
Jan am 22 Jan. 2019
Bearbeitet: Jan am 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 Kommentare
Ashok Das
Ashok Das am 22 Jan. 2019
I don't want to delete the whole field.
In the given example I just want to delete the first values of type and profile.
Jan
Jan am 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.

Melden Sie sich an, um zu kommentieren.


Laxmikant Sharma
Laxmikant Sharma am 13 Jul. 2022
Bearbeitet: Laxmikant Sharma am 13 Jul. 2022
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
Thanks @Jan to make it clearer, edited accordingly.
Now...the updated ize of s will be
size: 1x4
  2 Kommentare
Jan
Jan am 13 Jul. 2022
I'd insert a comma for clarity:
s = s([1:a - 1, a + 1:end])
But this looks easier:
s(a) = [];
Stephen23
Stephen23 am 26 Dez. 2022
Bearbeitet: Stephen23 am 26 Dez. 2022
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"
No, that is incorrect.
The code shown will remove that element and change the size of the structure array:
s = struct('a',cell(1,5))
s = 1×5 struct array with fields:
a
s(2) = [] % changes the size
s = 1×4 struct array with fields:
a
So your incorrect statement is very easy to disprove.

Melden Sie sich an, um zu kommentieren.


Ala'a Alshubbak
Ala'a Alshubbak am 25 Dez. 2022
what about only delete one element not all the field.
for example delete
S.population(2).type=2 so instead to have 2 it will not presents . would that be possile
specailly in a structure of different types of element , not homogenous
  1 Kommentar
Walter Roberson
Walter Roberson am 25 Dez. 2022
non-scalar struct arrays must all have the same fields (in the same order).
You could replace the 2 with something like [] but if S.population(1) has a direct field names type then S.population(2) would have to have it as well.
The fields are not required to be the same type between the different entries, so you could have
S.population(1).key = 'Redding';
S.population(1).variety = 'person';
S.population(1).variants.firstname = 'Otis';
S.population(2).key = 'Sitting';
S.population(2).variety = 'song';
S.population(2).variants.title = "(Sittin' on) The Dock of the Bay";
This is valid because the field names directly present in the nonscalar struct array are all the same. S.population(1).variants and S.population(2).variants do not need to have the same datatypes

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Structures finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by