删除数组结构 MATLAB 中的 [ ] 行

Deleting [ ] Rows in Array Struct MATLAB

我有一个包含许多 1x#(数据)结构的 24x1(轨道)结构。它们类似于:

Lat    Lon   Date
------------------
40.1, -53.5  736257

33.8, -52.3  736255

41.6, -50.1  736400

39.5, -48.4  735600

我想从结构中删除特定日期以过滤一些数据。我在其中做了:

for i= 1:length(track)
   for j= 1:length(track(i).data)
    strdate = track(i).data(j).Date;

    if strdate == 736257 

        track(i).data(j).Date = [];
        track(i).data(j).Lat = [];
        track(i).data(j).Lon = [];


    end
  end
end

这给我留下了整个结构中的各种 [ ] 行,而不是我真正想要的。我想完全删除这些行(显然知道结构的大小会改变)。我该怎么做呢?

如果要彻底清除数据单元格中所有无效的j坐标,可以尝试以下方法:

for i= 1:length(track)
    %Initialize a mask which marks the indices which should be kept with true
   indsToKeep = false(length(track(i).data),1);

   for j= 1:length(track(i).data)
       strdate = track(i).data(j).Date;
       %if strdate is relevant, mark the corresponding coordinate as true
       if strdate ~= 736257 
           indsToKeep (j) = true;
       end
   end
   %deletes all the irelevant structs from track(i).data, using indsToKeep 
   track(i).data = track(i).data(indsToKeep);
end

不过,如果你只是想擦除无关坐标的'Date'、'Lon'、'Lat'字段,你可以使用rmfield函数。

代码示例:

%generates a strct with two fields, and prints it
strct.f1=1;
strct.f2=2;
strct
%remove the first field and prints it
strct2 = rmfield(strct,'f1');
strct2

结果:

strct = 

    f2: 2
    f1: 1


strct2 = 

    f2: 2

在你的情况下:

track(i).data(j) = rmfield(track(i).data(j),'Date');
track(i).data(j) = rmfield(track(i).data(j),'Lat');
track(i).data(j) = rmfield(track(i).data(j),'Lon');