該解決方案提供了我認爲您需要的東西;一些言論,我希望對大家有用是上側
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);
結果
Datetime Data
2007-01-01 00:00 100
2007-02-01 00:00 200
2007-03-01 00:00 300
編輯:(從評論)在N-立柱電池一般情況下,你可以簡單地去一個for循環,例如
for i = 1 : size(Final,1)
fprintf(fid,'%s ', Final{i,:});
fprintf(fid,'\n');
end
(結果相同,但不依賴於列數)。
+1爲您提供的易於使用的代碼 – Acorbe