Removing NaNs from a struct
    11 visualizzazioni (ultimi 30 giorni)
  
       Mostra commenti meno recenti
    
    Manny Kins
 il 1 Mag 2019
  
    
    
    
    
    Modificato: Walter Roberson
      
      
 il 8 Apr 2020
            I have a struct with the following layout:
T      X      Y
[0,1] [0, 1] [0,5]
[0]    [2]     [2]
NaN	NaN	NaN
NaN	NaN	NaN
NaN	NaN	NaN
NaN	NaN	NaN
[0]    [3]    [3]
Is there a way to remove all NaN values such that I am left with only:
T      X      Y
[0,1] [0, 1] [0,5]
[0]    [2]    [2]
[0]    [3]    [3]
I am using isnan but keep getting an error "Undefined function 'isnan' for input arguments of type 'struct'". 
Thanks
4 Commenti
  Walter Roberson
      
      
 il 1 Mag 2019
				Probably the easiest way is to use struct2cell and cellfun(@(C) any(isnan(C(:)), thecell), and then any() that across the proper dimension, to arrive at a logical vector of location to remove. Then delete those from the original struct.
Risposta accettata
  Stephen23
      
      
 il 1 Mag 2019
        
      Modificato: Stephen23
      
      
 il 1 Mag 2019
  
      This works for any sized fields (including empty), and gives you the choice of matching ALL or ANY of the fields containing NaN.
>> F = @(s)all(structfun(@(a)isscalar(a)&&isnan(a),s)); % or ANY
>> X = arrayfun(F,AllData.Passive);
>> AllData.Passive(X) = [];
checking:
>> AllData.Passive.T
ans =
     0     1     2     3     4     6     7     8     9    10
ans =
     0     3     4     7     9    10
ans =
     0     2     6
ans =
     0     1     6     7     8
ans =
    10
ans =
    10
ans =
    10
ans =
    10
2 Commenti
  Yago Molano Gomez
 il 7 Apr 2020
				Hi! I've tried to copy this into my code but changing the empty brackets for a zero, and I get an error that says 'assignment between unlike types is not allowed'.
Also, when I use it with the empty brackets, it does not give me an error but does not remove the NaN's. How can I fix it? I'll attach the structures I'm referring to. 
Più risposte (2)
  Jos (10584)
      
      
 il 1 Mag 2019
        TF = arrayfun(@(k) isnan(AllData.Passive(k).T(1)), 1:numel(AllData.Passive))
AllData.Passive(TF) = []
  Felipe Ademir aleman hernandez
 il 8 Apr 2020
        
      Modificato: Walter Roberson
      
      
 il 8 Apr 2020
  
      Hey, this works for me:
MyNewDataStruct = structfun( @rmmissing , MyDataStruct , 'UniformOutput' , false)
0 Commenti
Vedere anche
Categorie
				Scopri di più su Cell Arrays in Help Center e File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!





