Delete row from a structure array
106 次查看(过去 30 天)
显示 更早的评论
For some reason, the solutions given on the internet don't seem to work for me. The most common solution I have seen is to just do what one would do for arrays. If 'a' is a structure array and I want to delete the 9th entry from all fields:
a(9)=[];
I get an error message saying "Matrix index is out of range for deletion."
What am I doing wrong? My strucutre array has both numeric and cell arrays. I tried removing the cell array field entirely and trying the above operation on the new array (which has only numeric fields), that didn't work either.
Thanks.
0 个评论
回答(1 个)
Matt J
2015-9-13
编辑:Matt J
2015-9-13
As far as can be seen, you simply aren't correctly assessing the number of elements in a. When a has 9 elements or more, as in the following example, things should work
>> clear a
>> a(20).numeric=123; a(20).cell={'dog','cat'}
a =
1x20 struct array with fields:
numeric
cell
>> a(9)=[]
a =
1x19 struct array with fields:
numeric
cell
2 个评论
Walter Roberson
2015-9-13
You might want to consider putting all of the numeric information together into one array, 6 x 53369 double, and accessing by rows when using the data. For example,
as_id = 1;
as_dtnum = 2;
as_lat = 3;
as_lon = 4;
as_sst = 5;
as_sss = 6;
ASSET.data(as_id,431) %equivalent to old ASSET.id(431)
and then you can delete all of the information for one asset by
ASSET.data(:,9) = [];
ASSET.type(9) = [];
(the cell needs to be maintained separately from the numeric values if you want easy numeric access to the numeric data.)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!