Matlab利用load循环读入数据文件


Contents

1.load循环读入.mat
2.1.fprintf写出数据txt
2.2.load循环读入txt


% 利用load循环读取文件
% 把文件的文件名按一定的规律命名,假如:f001.mat,f002.mat,...
% 在读取的时候则可以使用循环


1.load循环读入.mat

% f001.mat, f002.mat, f003.mat三个数据文件,其变量名称均为a(1x3的矩阵)

a = [0 0 1];
save f001;
a = [0 0 2];
save f002;
a = [0 0 3];
save f003;

 

clear;

 

x = zeros(3);   %将三个文件的数据读到 x 中
for i = 1:3
    FileName = ['f00' num2str(i)];
    load (FileName);
    x(i, :) = a;    %a是load进入的变量名
end

disp('x = ');
disp(x);


x =
     0     0     1
     0     0     2
     0     0     3

 

2.1.fprintf写出数据txt

a1 = 1:10;
a2 = 11:20;

fid1 = fopen('t001.txt','wt');   %若是在txt中,需在w后面加上t,由w变为wt,n才能识别
for i=1:10
    fprintf(fid1,'%g n',a1(i));
end
fclose(fid1);

fid2 = fopen('t002.txt','wt');   %若是在txt中,需在w后面加上t,由w变为wt,n才能识别
for i=1:10
    fprintf(fid2,'%g n',a2(i));
end
fclose(fid2);


2.2.load循环读入.txt

y = zeros(10,2);
for i = 1:2
    FileName = ['t00' num2str(i) '.txt'];
    a = load (FileName);
    y(:, i) = a;
end

disp('y = ');
disp(y);


y =
     1    11
     2    12
     3    13
     4    14
     5    15
     6    16
     7    17
     8    18
     9    19
    10    20


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM