I have a matrix:
raw =
'alon' 'tomer' 'ilana' 'T1' '2' '53' '24' 'NaN'
'ilana' 'oren' '6' 'a' 'g' 'g' 'gsh' 'NaN'
'dikla' 'aba' 'mama' 'a' 'h' 'ghfs' 'bfsbf' 'NaN'
'4' 'NaN' 'NaN' 'nn' 'NaN' 'NaN' 'hadhd' 'NaN'
and I want to remove NaN and to get:
'alon' 'tomer' 'ilana' 'T1' '2' '53' '24'
'ilana' 'oren' '6' 'a' 'g' 'g' 'gsh'
'dikla' 'aba' 'mama' 'a' 'h' 'ghfs' 'bfsbf'
'4' '' '' 'nn' '' '' 'hadhd'
how do I do that?
I tried a lot of suggestions, but I got errors:
1)
>> raw=raw(~isnan(raw(:,2)),:)
??? Undefined function or method 'isnan' for input arguments of type 'cell'.
2)
raw(isnan(raw(:,1)),:) = [];
??? Undefined function or method 'isnan' for input arguments of type 'cell'.
3)
raw(~any(isnan(raw),2),:)
??? Undefined function or method 'isnan' for input arguments of type 'cell'.
4)
T(cellfun(@isnan,T))={0}
??? Error using ==> cellfun
Non-scalar in Uniform output, at index 1, output 1.
Set 'UniformOutput' to false.
I tried Ansari's solution, but now I got:
raw =
'alon' 'tomer' 'ilana' 'T1' '2' '53' '24' [0]
'ilana' 'oren' '6' 'a' 'g' 'g' 'gsh' [0]
'dikla' 'aba' 'mama' 'a' 'h' 'ghfs' 'bfsbf' [0]
'4' [ 0] [ 0] 'nn' [0] [ 0] 'hadhd' [0]
it's not good for me, because I want to do:
size(raw,2)
and to get:
7
Something like this will work:
f = @(x) strcmp(x, 'NaN');
nans = cellfun(f, raw);
raw(nans) = {''}; %cell(sum(nans(:)), 1);
isnan
only works on matrices, so it won't work on cell arrays. Also your NaNs seem to be strings, not real NaNs.
At this point, all the 'NaN's will be empty strings. The following will remove entire rows or columns of empty strings:
raw = raw(:, arrayfun(@(i) length([raw{:, i}]), 1:size(raw, 2)) > 0); % for columns
raw = raw(arrayfun(@(i) length([raw{i, :}]), 1:size(raw, 1)) > 0, :); % for rows
For the last step though you might as well loop to keep the code clean and understandable (and easier to debug!). You're basically collapsing each row or each column by concatenating the strings, then checking if the length is zero, and if it is you're removing that row or column.