MATLAB: Finding out number of columns with no NaNs

for loopnan

Hello,
I have a large dataset with 923 columns x 56 rows with some missing values (NaN) present. Ultimately I would like to find out the number of columns with no NaNs present in any of the 56 rows.
The data is in a structure flow.data and the column headers are in flow.textdata. I would like to create a new structure which only includes the columns without NaNs and has each column as a separate field with the corresponding column header as the fieldname.
I have written this bit of code but it's not really doing the job. It writes 923 fields in the new structure and in those fields it writes all of the original data (a 56×923 matrix). Could you please help me fix the code or suggest a better way to do things. Also, I was wondering how I can find out the number of fields in the new structure once I have created it successfully? I really appreciate the help!
varnames=flow.textdata(1:end);
for n=1:56 %number of rows
for i=1:923 %number of columns
for k=1:length(varnames);
if annual_flow(:,i) == NaN;
break
else annual_flow(:,i) ~= NaN %not equal to NaN;
new_structure.(varnames{k})(n,i)=annual_flow(n,i);
end
end
end
end
end

Best Answer

t = ~any(isnan(flow.data));
a2 = flow.data(:,t);
c2 = [flow.textdata(t);mat2cell(a2,size(a,1),ones(size(a2,2),1))];
new_struct = struct(c2{:});