2016-04-07 61 views
0

我有一個包含多個1x#(數據)結構的24x1(軌道)結構。他們是與此類似:刪除[]數組結構中的行MATLAB

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 

這使我在整個結構中留下了各種各樣的行,而不是我實際上想要的。我想完全刪除這些行(顯然知道結構的大小會改變)。我會如何去做這件事?

+0

請提供[MCVE。 – excaza

+0

@excaza我添加了一個結構的例子。試圖保持簡單 – Constantine

回答

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 

但是,如果你只是想刪除的「日期」,「經度」,和'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'); 
+0

糾正我,如果我錯了,但我認爲你混淆了真假'indsToKeep = true(長度(track(i).data),1);' 更多代碼 '如果strdate〜 = 736257 indsToKeep(j)= true; end' – Constantine

+0

它應該可以工作。您可以將初始數組設置爲true值,並在需要時將其更改爲false值,或者嘗試其他方法。 – drorco

+0

我提出的唯一原因是因爲生成的結構實際上是我想排除的所有日期。 – Constantine