将组合字符串和数字输入的单元格数组写入文本文件

Kat*_*tyB 6 matlab printf

考虑以下:

DateTime = {'2007-01-01 00:00';'2007-02-01 00:00';'2007-03-01 00:00'};
Headers = {'Datetime','Data'};
Dat = [100,200,300];

Data = [DateTime,num2cell(Dat')];
Final = [Headers;Data];
Run Code Online (Sandbox Code Playgroud)

如何将"Final"中的数据写入制表符分隔的文本文件中.我知道如何在变量仅由数字输入组成时使用fopen,fprintf等,但我正努力解决这个问题.我试过了:

fid = fopen('C:\Documents\test.txt','wt');
fprintf(fid,'%s\t%s\n',Final{:});
fclose(fid);
Run Code Online (Sandbox Code Playgroud)

但是,这不会生成与matlab中生成的文本格式相同的文本文件.怎样才能解决这个问题?

Aco*_*rbe 6

该解决方案提供了我认为您需要的功能; 我希望有用的一些评论是正确的

 DateTime = {'2007-01-01 00:00';'2007-02-01 00:00';'2007-03-01 00:00'};
 Headers = {'Datetime','Data'};
 Dat = [100,200,300];


 % // In the way you used fprintf it expects just strings ('%s\t%s\n'), 
 % // therefore Data should be composed exclusively by them.
 % // Numbers are converted to strings by using num2str
 % // by using cellfun we iteratively convert every element of num2cell(Dat')
 % // in strings, obtaining a cell
 Data = [DateTime,cellfun(@num2str, num2cell(Dat'), 'UniformOutput' , false )]; 
 Final = [Headers;Data];

 fid = fopen('test.txt','wt');

 % // this iterates fprintf on the cell rows, giving you the output
 cellfun(@(x,y) fprintf(fid,'%s\t%s\n',x,y),Final(:,1),Final(:,2));       
 fclose(fid);
Run Code Online (Sandbox Code Playgroud)

结果

 Datetime   Data
 2007-01-01 00:00   100
 2007-02-01 00:00   200
 2007-03-01 00:00   300
Run Code Online (Sandbox Code Playgroud)

编辑:(来自评论)在一个N列单元格的一般情况下,你可以简单地去一个for循环,例如

for i = 1 : size(Final,1)
    fprintf(fid,'%s ', Final{i,:} );
    fprintf(fid,'\n');
end
Run Code Online (Sandbox Code Playgroud)

(结果相同,但不取决于列数).