how to read multiple file from single .mat file

1 Ansicht (letzte 30 Tage)
Sun Heat
Sun Heat am 9 Jun. 2021
Kommentiert: Sun Heat am 11 Jun. 2021
i have 16 different file in "data2-18.mat", like M1,M2,M3,M4.......M16. i want to call every file(i.e., M2 ,M6, M5...) in loop according to variable "A" (in program).
clc;clear all;
load data2-18
A=[2 6 5 8 9];
for i=1:5
data1 = M(A(i));
F1=data1(:,1);
E1=data1(:,2);
Ei1=data1(:,3);
M1=data1(:,4);
Mi1=data1(:,5);
Ereal=complex(E1,Eimg1);
Mreal=complex(M1,Mimg1);
end

Akzeptierte Antwort

Jan
Jan am 9 Jun. 2021
Bearbeitet: Jan am 9 Jun. 2021
I assume you use the term "file", but you mean "variable".
Calling load() without catching the output, creates variables dynamically. This has a lot of severe disadvantages. One of the worst is that it impedes debugging, because you cannot know be reading the code, where a variable is coming from. Better:
FileData = load('data2-18.mat');
A = [2 6 5 8 9];
for i = 1:5
data1 = FileData.(sprintf('M%d', i));
...
Avoid hiding indices in names of variables. Use arrays an indices.

Weitere Antworten (0)

Produkte

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by