簡體   English   中英

matlab,如何將文本和數值數據導出到excel csv文件?

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

當我使用下面的代碼。 我得到了正確導出的數值數據,但是當每列的第一行應該有標題(標題)時,第一行是空白的。 有解決方案嗎

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

在原始的例子中

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM