Delete row from a structure array

77 views (last 30 days)
bertie
bertie on 13 Sep 2015
Commented: Walter Roberson on 13 Sep 2015
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.

Answers (1)

Matt J
Matt J on 13 Sep 2015
Edited: Matt J on 13 Sep 2015
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 Comments
bertie
bertie on 13 Sep 2015
Thanks, you are right. Turns out my I have a struct variable (ASSET) that is not an array but has fields that are arrays. What I mean is the size of ASSET is 1x1 but I can access individual entries using ASSET.id(9), say.
ASSET =
id: [1x53369 double]
dtnum: [1x53369 double]
lat: [1x53369 double]
lon: [1x53369 double]
type: {1x53369 cell}
sst: [1x53369 double]
sss: [1x53369 double]
I guess the solution is to delete the rows by accessing each individual field:
ASSET.id(rows to delete)=[] ;
ASSET.dtnum(rows to delete)=[]
.
. and so on.
Not the most elegant way to do things but will work.
Walter Roberson
Walter Roberson on 13 Sep 2015
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.)

Sign in to comment.

Categories

Find more on Data Types in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!