Hi all,
I have the following code for finding a NaN value in a structure:
structure.w.c = 5; structure.w.d = 4; structure.x.a = 1; structure.y.a = NaN; structure.z.a = 3; first_level = fieldnames(structure); structure_path_first = strcat('structure.',first_level); structure_path_complete = {}; for i=1:1:length(first_level) temp = eval(structure_path_first{i}); second_level{i} = fieldnames(temp); if strcmp(second_level{1,i},'a') structure_path_complete{i} = strcat(structure_path_first{i},'.',second_level{i}); temp_2 = eval(char(structure_path_complete{i})); if isnan(temp_2) value_nan{i} = structure_path_complete{i}; end end end
The problem of the structure is that I do not know the names of the second level (w,x,y,z) and also not the number of array fields (a,d,c) in advance.
The code is working fine and I get the complete names of the array fields containing a NaN. The question now is, is there a smarter way than using for-loops finding these fields?
Thank you for your answers.
Cheers
Christian
Best Answer