str2double / str2num / conversion confusion

조회 수: 1 (최근 30일)
Golnar
Golnar 2014년 3월 27일
댓글: Golnar 2014년 3월 28일
f1=fopen('report_data.txt', 'r');
f2=fopen('imaginary.txt', 'r');
area=[str2double(fgetl(f1)), str2double(fgetl(f2))]';
volume=[str2double(fgetl(f1)), str2double(fgetl(f2))]';
healthy_exposed=[str2double(fgetl(f1)), str2double(fgetl(f2))]';
pus=[str2double(fgetl(f1)), str2double(fgetl(f2))]';
necrotic=[str2double(fgetl(f1)), str2double(fgetl(f2))]';
fprintf('Surface Area: %2.1f cm^2 \n', str2double(area(1)));
fprintf('Volume: %2.3f cm^3 \n', str2double(volume(1)));
fprintf('Necrotic: %2.3f', str2double(necrotic(1)));
fprintf('Infection: %2.3f', str2double(pus(1)));
fprintf('Exposed: %2.3f', str2double(healthy_exposed(1)));
Result:
The initial format is a string because its a line from an edit box in a GUI. But these 5 parameters are numbers. Why aren't they showing up?

채택된 답변

Joseph Cheng
Joseph Cheng 2014년 3월 28일
편집: Joseph Cheng 2014년 3월 28일
reason is that you are doing str2double 2x. so area already has the text being converted to a double. then you try to convert a double to double in your fprintf.
simple test:
x='1';
str2double(str2double(x))
will result to NaN
no need for the str2double in the fprintf statement
  댓글 수: 2
Golnar
Golnar 2014년 3월 28일
How right you are!
Golnar
Golnar 2014년 3월 28일
Yet for one of the parameters it still results NaN

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by