필터 지우기
필터 지우기

How to delete elements from structure

조회 수: 558 (최근 30일)
Ashok Das
Ashok Das 2019년 1월 22일
편집: Stephen23 2022년 12월 26일
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
Jan
Jan 2019년 1월 22일
By the way, "cell" is a bad choice for a variable, because this shadows the important Matlab function cell().
Stephen23
Stephen23 2019년 1월 23일
편집: Stephen23 2019년 1월 23일
"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.

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2019년 1월 22일
편집: Stephen23 2019년 1월 22일
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
Jan
Jan 2019년 1월 22일
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 2019년 1월 23일
This is working. Thanks.

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Jan
Jan 2019년 1월 22일
편집: Jan 2019년 1월 22일
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
Ashok Das
Ashok Das 2019년 1월 22일
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 2019년 1월 22일
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
Laxmikant Sharma 2022년 7월 13일
편집: Laxmikant Sharma 2022년 7월 13일
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
Jan
Jan 2022년 7월 13일
I'd insert a comma for clarity:
s = s([1:a - 1, a + 1:end])
But this looks easier:
s(a) = [];
Stephen23
Stephen23 2022년 12월 26일
편집: Stephen23 2022년 12월 26일
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.

댓글을 달려면 로그인하십시오.


Ala'a Alshubbak
Ala'a Alshubbak 2022년 12월 25일
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
Walter Roberson
Walter Roberson 2022년 12월 25일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by