[英]Using fprintf for csv in matlab
I would like to generate a csv file with a header and some data. 我想生成带有标头和一些数据的csv文件。 Since I am on a Mac, this does not work using csvwrite, so I need to use fprintf.
由于我在Mac上,因此无法使用csvwrite进行工作,因此我需要使用fprintf。
I am storing my headers in a cell array called header (header = 'AB' 'CB'). 我将标头存储在称为标头的单元格数组中(标头='AB''CB')。 Whatever I am trying I get an error:
无论我在尝试什么,都会收到错误消息:
Error using fprintf
使用fprintf时出错
Function is not defined for 'cell' inputs.
未为“单元”输入定义功能。
Can somebody make an easy example with the header example? 有人可以用标头示例举一个简单的示例吗?
Something like this should work, 这样的事情应该起作用,
headers ={'Header1','Header2','Header3'};
fh = fopen('SomeFile.csv','w');
for ii = 1:numel(headers)
if ii == 1
fprintf(fh,headers{ii});
else
fprintf(fh,[',',headers{ii}]);
end
end
randVals = rand(5,3);
for ii = 1:size(randVals,1)
fprintf(fh,'\n');
for jj = 1:size(randVals,2)
if jj == 1
fprintf(fh,num2str(randVals(ii,jj)));
else
fprintf(fh,[',',num2str(randVals(ii,jj))]);
end
end
end
fclose(fh)
You just need to separate out the headers and do the manual grunt work that csvwrite would normally do for you. 您只需要分离出头文件并执行csvwrite通常为您完成的手动操作即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.