cellfun(@isnan) and erasing NaN

Dear all,
I have a matrix like this in excel file:
A=[a, 1, NaN, 1, 1, 1, 1;...
b, 2, NaN, NaN, NaN, NaN, NaN;...
c, 3, NaN, 3, 3, 3, 3];
and I want to erase the rows in which has NaN starting from fourth column. In another words I would like to have
B=[a, 1, NaN, 1, 1, 1, 1;...
c, 3, NaN, 3, 3, 3, 3];
I used this:
[ndata, text, a] = xlsread('test.xls');
xlswrite('newfile.xls', a(~any(cellfun(@isnan,a), 2), :));
But it works just in cases of having numbers in the excel file. But with the above example ,in which I have a, b,and c, I receive this error:
Error using cellfun Non-scalar in Uniform output, at index 1, output 1. Set 'UniformOutput' to false.
Or if you have other idea to do this operation please let me know.

 채택된 답변

Sean de Wolski
Sean de Wolski 2013년 2월 19일

2 개 추천

Close!
A={'a', 1, NaN, 1, 1, 1, 1;...
'b', 2, NaN, NaN, NaN, NaN, NaN;...
'c', 3, NaN, 3, 3, 3, 3};
B = A(~any(cellfun(@isnan,A(:,4:end)),2),:)

댓글 수: 3

Victor
Victor 2013년 2월 19일
편집: Andrei Bobrov 2013년 2월 20일
can you suggest writing that in form of "xlswrite"!!
[ndata, text, a] = xlsread('test.xls');
xlswrite('newfile.xls', a(~any(cellfun(@isnan,a(:,4:end)),2),:));
Because it gives:
Error using cellfun Non-scalar in Uniform output, at index 2, output 1. Set 'UniformOutput' to false.
Sean de Wolski
Sean de Wolski 2013년 2월 20일
Break it into pieces.
What does whos() return after running the first line?
Then run the line I have. Does it work?
If not, why not? If so, then worry about xlswrite()
Victor
Victor 2013년 2월 20일
Thanks, now it works.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 2월 19일
편집: Azzi Abdelmalek 2013년 2월 19일

2 개 추천

Use
cellfun(@isnan,a,'UniformOutput',false)
Which means
xlswrite('newfile.xls', a(~any(cellfun(@isnan,a,'UniformOutput',false), 2), :));

댓글 수: 7

Victor
Victor 2013년 2월 19일
Thanks, but:
Undefined function 'any' for input arguments of type 'cell'.
Azzi Abdelmalek
Azzi Abdelmalek 2013년 2월 19일
편집: Azzi Abdelmalek 2013년 2월 19일
Then use
xlswrite('newfile.xls', a(~any(cell2mat(cellfun(@isnan,a,'un',0)), 2), :));
Victor
Victor 2013년 2월 19일
Thanks
xlswrite('newfile.xls', a(~any(cell2mat(cellfun(@isnan,a,'un',0)), 2), :));
Error using cat Dimensions of matrices being concatenated are not consistent.
Azzi Abdelmalek
Azzi Abdelmalek 2013년 2월 19일
편집: Azzi Abdelmalek 2013년 2월 19일
I've tried to fellow your code, now try this
A={'a', 1, NaN, 1, 1, 1, 1
'b',2,NaN,NaN,NaN,NaN,NaN
'c',3,NaN,3,3,3,3}
b=A(:,4:end)
A=A(~all(cellfun(@isnan,b),2),:)
Victor
Victor 2013년 2월 20일
I think I am doing something wrong... When I read the file with "xlsread" that is in not in the format of cell. By the way I tried to use "cell2mat" so that I can write it with xlswrite, but " All contents of the input cell array must be of the same data type."
a=cell2mat(a)
xlswrite('newfile.xls',a);
Azzi Abdelmalek
Azzi Abdelmalek 2013년 2월 20일
You have not to use cell2mat before xlswrite
Victor
Victor 2013년 2월 20일
Thanks Azzi, but to be honest, I do not think with this method, I can read the file, do the manipulation and then write it back.

댓글을 달려면 로그인하십시오.

카테고리

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by