matlab中写存txt文件,如何在MATLAB中的.txt文件中保存数据

首先创建输出矩阵,然后将其写入文件.

这是新代码:

BaseFile ='s';

n=3;

for i=1:n % it's not recommended to use i or j as variables, since they used in complex math, but I'll leave it up to you

% Open each file consecutively

d=fopen([BaseFile num2str(i) '.txt']);

% read data from file

A=textscan(d,'%f%f', 'CollectOutput',1);

% sort the data according to the 1st column

B=sortrows(A{:},1);

% Instead of deleting a column create new matrix

if(i==1)

C = zeros(size(B,1),n);

end

% Check input file and save the 2nd column

if size(B,1) ~= size(C,1)

error('Input files have different number of rows');

end

C(:,i) = B(:,2);

% don't write yet

fclose (d);

end

% write to a new file

fid=fopen('RT.txt','w');

for k=1:size(C,1)

fprintf(fid, [repmat('%d\t',1,n-1) '%d\n'], C(k,:));

end

fclose(fid);

编辑:

实际上只将数字写入一个不需要FPRINTF的文件.请改用DLMWRITE:

dlmwrite('RT.txt',C,'\t')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值