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