matlab中的制表符分隔文本文件

matlab中的制表符分隔文本文件,matlab,file-io,text-files,Matlab,File Io,Text Files,以下代码生成了与我当前使用的数据集类似的数据集: clear all a = rand(131400,12); DateTime=datestr(datenum('2011-01-01 00:01','yyyy-mm-dd HH:MM'):4/(60*24):... datenum('2011-12-31 23:57','yyyy-mm-dd HH:MM'),... 'yyyy-mm-dd HH:MM'); DateTime=cellstr

以下代码生成了与我当前使用的数据集类似的数据集:

clear all
    a = rand(131400,12);
    DateTime=datestr(datenum('2011-01-01 00:01','yyyy-mm-dd HH:MM'):4/(60*24):...
        datenum('2011-12-31 23:57','yyyy-mm-dd HH:MM'),...
        'yyyy-mm-dd HH:MM');
    DateTime=cellstr(DateTime);
    header={'DateTime','temp1','temp2','temp4','temp7','temp10',...
        'temp13','temp16','temp19','temp22','temp25','temp30','temp35'};
我试图将输出转换为一个变量(称为“Data”),即将header作为第一行(1,:),“DateTime”从第2行(2:end,1)开始,并贯穿每一行,最后将“a”作为数据(2:end,2:end),如果这有意义的话。因此,“DateTime”和“header”分别用作行和列的标题。接下来,我需要将其保存到一个以制表符分隔的文本文件中


我希望我已经清楚地表达了我的意图

一种简单的方法,但可能不是最快的:

Data = [header; DateTime, num2cell(a)];
filename = 'test.txt';
dlmwrite(filename,1); %# no create text file, not Excel
xlswrite(filename,Data);

更新: 似乎
xlswrite
实际上更改了
DateTime
值的格式,即使它写入文本文件也是如此。如果格式很重要,以下是更好、更快的方法:

filename = 'test.txt';
out = [DateTime, num2cell(a)];
out = out'; %# our cell array will be printed by columns, so we have to transpose

fid = fopen(filename,'wt');
%# printing header
fprintf(fid,'%s\t',header{1:end-1});
fprintf(fid,'%s\n',header{end});
%# printing the data
fprintf(fid,['%s\t', repmat('%f\t',1,size(a,2)-1) '%f\n'], out{:});
fclose(fid);

这可能会更清楚:给出数据变量的前几行预期输出。