Using fprintf: string in array is NaN

7 vues (au cours des 30 derniers jours)
Lisa M
Lisa M le 5 Fév 2020
Modifié(e) : Stephen23 le 5 Fév 2020
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);

Réponse acceptée

Stephen23
Stephen23 le 5 Fév 2020
Modifié(e) : Stephen23 le 5 Fév 2020
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

Plus de réponses (0)

Catégories

En savoir plus sur Convert Image Type dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by