for i=1:length(Table1.Columnname)
if isnan(Table1.Columnname(i))
Table1.Columnname(i) = 'NO';
end
end
I have the above code but it does not work. I think it has to do with how I have defined Table1.Columnname(i) = 'NO';
Please can someone tell me how to code this properly for my isnan i.e. to set that cell in the table to "NO"?

1 comentario

dpb
dpb el 10 de Abr. de 2014
Format your code so it's legible, please...
But, the problem is more than likely that since you have a table, there's no NaN but a string 'NaN' is my guess. Have you tested w/ debugger or w/ debugging output what
isnan(Table1.Columnname(i))
returns for the cell in question?

Iniciar sesión para comentar.

 Respuesta aceptada

Walter Roberson
Walter Roberson el 10 de Abr. de 2014

0 votos

What datatype is Table1.Columnname ? If it is a char array (string) then you cannot store two elements 'N' and 'O' in one location. If it is a cell array then you need to work with its contents rather than with the cell, such as Table1.Columnname{i} = 'NO' . And as Duane points out, you need to know whether the cell would hold NaN (a numeric value) or 'NaN' (a string)

6 comentarios

Matthew
Matthew el 10 de Abr. de 2014
Thank you Walter / Duane for your reply.
Okay - I have imported this table into Matlab and the field is a cellVector. But then I converted the text string to numbers. This code works perfectly fine when replacing 'NO' with e.g. 7. Any suggestions please?
Walter Roberson
Walter Roberson el 10 de Abr. de 2014
When you convert the text string to numbers, what value do you want the 'NO' to become?
Matthew
Matthew el 11 de Abr. de 2014
The source data is a mixture of numbers and NaN. After the text string to numbers conversion, the source data is still numbers and NaN. Now I want to convert where there is a NaN to set to a text 'NO'. This is the difficulty I am experiencing.
Walter Roberson
Walter Roberson el 11 de Abr. de 2014
YourCellArray(cellfun(@isnan, YourCellArray)) = {'NO'};
Matthew
Matthew el 14 de Abr. de 2014
Thanks Walter!
Image Analyst
Image Analyst el 14 de Abr. de 2014
I'm confused. Do you really have a table? How did you import? Did you do
T = readtable(yourFileName);
? Let's use the example in the help, and replace one of the items in the cell with nan and see what happens:
T = table(['M';'F';'M'],[45;32;34],...
{'NY';nan;'MA'},logical([1;0;0]),...
'VariableNames',{'Gender' 'Age' 'State' 'Vote'})
class('T.State') % Reports char.
whos T.State % Strangely silent - reports nothing.
YourCellArray = T.State % Extract column 3 into a cell array.
% The following line bombs.
YourCellArray(cellfun(@isnan, YourCellArray)) = {'NO'};
Can anyone explain why (1) whos is silent, and (2) why Walter's code bombs?

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Etiquetas

Preguntada:

el 10 de Abr. de 2014

Comentada:

el 14 de Abr. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by