当我使用下面的代码时。我得到了正确导出的数字数据,但是当每列的第一行应该有标题(标题)时,第一行是空白的。任何解决方案?
clc
clear
filename = 'file.csv';
fileID = fopen(filename,'wt');
%**************************************************************************
%Sample data (note sample data are transposed row, i.e. columns)
sample1 = [1,2,3,4]';
sample2 = [332.3, 275.8, 233.3, 275]';
sample3 = [360, 416.7, 500, 360]';
sample4= [-0.9, -0.9, -0.9, -0.9]';
sample5 = [ 300000, 0, 0, 0]';
A ={'text1','text2', 'text3', 'text4', 'text5'}; %'
%***************************************************************************
%write string to csv
[rows, columns] = size(A);
for index = 1:rows
fprintf(fileID, '%s,', A{index,1:end-1});
fprintf(fileID, '%s\n', A{index,end});
end
fclose(fileID);
%***************************************************************************
%write numerical data to csv
d = [sample1, sample2, sample3, sample4, sample5];
csvwrite(filename, d, 1);
最佳答案
您在编写数字数据时正在擦除字符串,将脚本运行到数字数据 - 它工作正常。只需切换操作顺序-先写数字,然后写字符串,它就会起作用。
编辑:
上面的解决方案不起作用,因为写入字符串会覆盖以前写入的数字数据,更简单直观的解决方案是替换
csvwrite(filename, d, 1);
经过
dlmwrite(filename, d,'-append', 'delimiter', ',');
在原始示例中
关于excel - matlab,如何将文本和数值数据导出到 excel csv 文件?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14165903/