MATLAB: How to remove columns that contain only NaNs from a cell array

allcellfunisnanMATLAB

I have a cell array that contains NaNs. Some columns only contain NaNs. How can I detect these columns and remove them from the cell array? 

Best Answer

% Create some data
A = {2, NaN, 3; NaN, NaN, 'text'; 5, NaN, 6};
% Create an anonymous function to detect NaNs
f = @(x) any(isnan(x));
% Detect where there are NaNs
B = cellfun(f, A);
% Now detect columns that contain ONLY NaNs
C = all(B);
% Remove these columns
A(:,C) = [];