删除Array Struct MATLAB中的[]行

时间:2016-04-07 15:55:20

标签: matlab structure rows

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

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

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

1 个答案:

答案 0 :(得分:0)

如果要完全删除数据单元格中的所有无效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

但是,如果您只想删除“日期”,“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');