matlab把文本数据存入数组,matlab – 将组合字符串和数字输入的单元格数组写入文本文件...

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

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值