Effacer les filtres
Effacer les filtres

cellfun(@isnan) and erasing NaN

19 vues (au cours des 30 derniers jours)
Victor
Victor le 19 Fév 2013
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.

Réponse acceptée

Sean de Wolski
Sean de Wolski le 19 Fév 2013
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 commentaires
Sean de Wolski
Sean de Wolski le 20 Fév 2013
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 le 20 Fév 2013
Thanks, now it works.

Connectez-vous pour commenter.

Plus de réponses (1)

Azzi Abdelmalek
Azzi Abdelmalek le 19 Fév 2013
Modifié(e) : Azzi Abdelmalek le 19 Fév 2013
Use
cellfun(@isnan,a,'UniformOutput',false)
Which means
xlswrite('newfile.xls', a(~any(cellfun(@isnan,a,'UniformOutput',false), 2), :));
  7 commentaires
Azzi Abdelmalek
Azzi Abdelmalek le 20 Fév 2013
You have not to use cell2mat before xlswrite
Victor
Victor le 20 Fév 2013
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.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by