Using fprintf: string in array is NaN
조회 수: 9 (최근 30일)
이전 댓글 표시
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);
댓글 수: 0
채택된 답변
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Convert Image Type에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!