필터 지우기
필터 지우기

Using fprintf: string in array is NaN

조회 수: 9 (최근 30일)
Lisa M
Lisa M 2020년 2월 5일
편집: Stephen23 2020년 2월 5일
Hi Everyone,
Please, see my code below. My data.name variable which is a string contains only NaN values when printed.
Any suggestions on how to solve this issue? Thanks in advance.
data.x= [0;1;0;1]
data.y= [1;1;1;1]
data.z= [0;0;0;0]
data.name={'Laura'; 'Caroline'; 'Lynne'; 'Charlie'}
input=[data.x data.y data.z convertCharsToStrings(data.name)];
fmt = repmat(',%.12g',1,size(input,2)); % numeric format for input
fmt = [fmt(2:end),'\n'];
[fid,msg] = fopen('test.csv','wt');
assert(fid>=3,msg)
fprintf(fid,'\n');
fprintf(fid,'\n');
fprintf(fid,fmt,input.');
fclose(fid);

채택된 답변

Stephen23
Stephen23 2020년 2월 5일
편집: Stephen23 2020년 2월 5일
No need to convert data to string:
>> data.x = [0;1;0;1];
>> data.y = [1;1;1;1];
>> data.z = [0;0;0;0];
>> data.name = {'Laura'; 'Caroline'; 'Lynne'; 'Charlie'};
>> tmp = [num2cell([data.x,data.y,data.z]),data.name].';
>> fmt = [repmat('%.12g,',1,3),'%s\n'];
>> fprintf(fmt,tmp{:})
0,1,0,Laura
1,1,0,Caroline
0,1,0,Lynne
1,1,0,Charlie

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Convert Image Type에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by