필터 지우기
필터 지우기

If isnan then set to text

조회 수: 1 (최근 30일)
Matthew
Matthew 2014년 4월 10일
댓글: Image Analyst 2014년 4월 14일
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
dpb
dpb 2014년 4월 10일
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?

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

채택된 답변

Walter Roberson
Walter Roberson 2014년 4월 10일
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
Matthew
Matthew 2014년 4월 14일
Thanks Walter!
Image Analyst
Image Analyst 2014년 4월 14일
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?

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by