Create loop to load .mat file and store values to a matrix.

5 Ansichten (letzte 30 Tage)
I have multiple .mat files with values for x and y. The variables in each file has the same name (x,y) , but different values.
I need to create a loop or a function that loads each file and will open them one at a time and save x and y (maybe in a matrix) in order to be able to plot them. Any thoughts?

Akzeptierte Antwort

Stephen23
Stephen23 am 7 Dez. 2021
Bearbeitet: Stephen23 am 7 Dez. 2021
This should get you started. In the absence of any data desription I assumed that withinin each file x and y are scalar. You will need to adapt to suit your filenames, data sizes, etc.:
P = 'absolute or relative file path to where the files are saved';
S = dir(fullfile(P,'*.mat'));
S = natsortfiles(S); % optional, if required download from FEX 47434.
for k = 1:numel(S)
F = fullfile(P,S(k).name);
S(k).data = load(F);
end
D = [S.data];
X = vertcat(D.x);
Y = vertcat(D.y);
plot(X,Y)

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by