简体   繁体   English

matlab,如何将文本和数值数据导出到excel csv文件?

[英]matlab, how to export text and numerical data to a excel csv file?

When I use the code below. 当我使用下面的代码。 I get the numerical data exported correctly however the first row is blank when there should be titles(headers) on the first row of each column. 我得到了正确导出的数值数据,但是当每列的第一行应该有标题(标题)时,第一行是空白的。 Any solutions? 有解决方案吗

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);

You are erasing strings when writing numerical data ,run your script just up to numerical data - it works fine. 您在编写数字数据时正在擦除字符串,只需运行您的脚本数字数据 - 它工作正常。 Simply switch order of operations- first write numbers, and then write strings, it will work. 只需切换操作顺序 - 首先写入数字,然后写入字符串,它就可以工作。

EDIT: Solution above won't work because writing string overwrites previously written numerical data, simpler and more intuitivesolution would be to replace 编辑:上面的解决方案将无法工作,因为写入字符串会覆盖以前编写的数字数据,更简单,更直观的解决方案将取代

csvwrite(filename, d, 1);

by 通过

dlmwrite(filename, d,'-append', 'delimiter', ',');

in original example 在原始的例子中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM