Load different files in a loop

I would like to open different files, and have the data plot in different subplots. Currently i have the code beneath for a specific file.
I hope you understand my idea.
My current code;
clear vars
close all
clc
%spoormodel='Spoormodel 01';
%spoormodel='Spoormodel 02';
%spoormodel='Spoormodel 03';
%spoormodel='Spoormodel 04';
%spoormodel='Spoormodel 05';
spoormodel='Spoormodel 06';
%spoormodel='Spoormodel 07';
%spoormodel='Spoormodel 08';
%spoormodel='Spoormodel 09';
%spoormodel='Spoormodel 10';
if isequal(spoormodel,'Spoormodel 01')
load ('Y:\Spoormodel\01\reactionforce.mat');
elseif isequal(spoormodel,'Spoormodel 02')
load ('Y:\Spoormodel\02\reactionforce.mat');
elseif isequal(spoormodel,'Spoormodel 03')
load ('Y:\Spoormodel\03\reactionforce.mat');
elseif isequal(spoormodel,'Spoormodel 04')
load ('Y:\Spoormodel\04\reactionforce.mat');
elseif isequal(spoormodel,'Spoormodel 05')
load ('Y:\Spoormodel\05\reactionforce.mat');
elseif isequal(spoormodel,'Spoormodel 06')
load ('Y:\Spoormodel\06\reactionforce.mat');
elseif isequal(spoormodel,'Spoormodel 07')
load ('Y:\Spoormodel\07\reactionforce.mat');
elseif isequal(spoormodel,'Spoormodel 08')
load ('Y:\Spoormodel\08\reactionforce.mat');
elseif isequal(spoormodel,'Spoormodel 09')
load ('Y:\Spoormodel\09\reactionforce.mat');
elseif isequal(spoormodel,'Spoormodel 10')
load ('Y:\Spoormodel\10\reactionforce.mat');
end
%reactionforce(4:2:end,:) = [];
%reactionforce(:,3:2:end) = [];
%thresholdmin = 1e14;
%thresholdmax = 1e15;
nancheck=~all(isnan(reactionforce{3:end,2:end}),1);
reactionforce = reactionforce(2:end,nancheck);
%reactionforce(all(reactionforce{:,2:end} < thresholdmin | reactionforce{:,2:end} > thresholdmax, 1), :) = [];
vars = reactionforce{1,2:end};
%vars = num2str(vars);
time = table2array(reactionforce(2:end,1));
data = reactionforce(2:end,1:end);
data = table2array(data(1:end,1:end));
cmap = colormap(parula(size(vars,2)));
subplot(211,'colororder',cmap);hold on
title(spoormodel);
plot (time,data)
legend( sprintf('%g\n', vars) )
grid on;
axis tight;
xlabel('tijd [s]')
ylabel('reactiekracht [N]')
hold on;

2 Comments

You should always load into an output variable, e.g.:
D = 'path of the directory where the subfolders 01, 02, etc. are';
N = 2;
for k = 1:N
F = sprintf('%02d',k);
S = load(fullfile(D,F,'reactionforce.mat'));
T = S.reactionforce;
... your code using table T.
end
If any data file is missing a particular variable then simply loading into the workspace will mean that the previous loop iteration's variable will be used, without any warning. Basically you will get meaningless results without knowing it. This is one reason why it is recommended to always load into an output variable.
Jesse
Jesse on 12 Nov 2018
Edited: Jesse on 12 Nov 2018
Okay, i understand your statement. Now i'm trying to solve it with your code. No i have the following problem; my first file is 06.mat, so it doesn't start at 01.mat, so the following error occurs;
Error using load Unable to read file 'X:\7. Spoormodel\data\01.mat'. No such file or directory.
Error in reactionforce_script_loop (line 9) S = load([D,F,'.mat']);
D = 'X:\7. Spoormodel\data\';
N = 2;
for k = 1:N
F = sprintf('%02d',k);
S = load([D,F,'.mat']);
T = S.reactionforce;
... your code using table T.
end

Sign in to comment.

Answers (1)

matfiles = dir('*.mat') ;
for i = 1:length(matfiles) ;
filename = matfiles(i).name ;
load(filename) ;
% do what you want
end

14 Comments

Like this ?
matfiles = dir('Y:\Spoormodel\gesimuleerd\*\reactionforce.mat') ;
for i = 1:length(matfiles) ;
filename = matfiles(i).name ;
load(filename) ;
% do what you want
I guess in this case i should add a path to the load command
KSSV
KSSV on 9 Nov 2018
Edited: KSSV on 9 Nov 2018
If you are not in the pwd...you should use:
matfiles = dir('Y:\Spoormodel\gesimuleerd\*\reactionforce.mat') ;
for i = 1:length(matfiles) ;
filename = [matfiles(i).folder, filesep,matfiles(i).name]
load(filename) ;
% do what you want
Jesse
Jesse on 9 Nov 2018
Edited: Jesse on 9 Nov 2018
Yes, that's the right start.
And, how can i get supplots from these mat files? I guess just a
(i)
everywhere doesn't work
Have a look on plot/ subplot.
It's almost working. Now it's giving 2 of the same...
for k = 1:i
subplot(2,2,k,'colororder',cmap);hold on
plot(time,data)
end
Check the files...they should differ in name.
Well, if i make the filenames different, it also doens't work.
Jesse
Jesse on 9 Nov 2018
Edited: Jesse on 9 Nov 2018
Almost there...
matfiles = dir('Y:\Spoormodel\rf\*.mat') ;
for i = 1:length(matfiles)
filename = [matfiles(i).folder,filesep,matfiles(i).name] ;
load(filename) ;
for k = 1:i
vars = reactionforce{1,2:end};
cmap = colormap(parula(size(vars,2)));
subplot(2,2,k,'colororder',cmap); hold on
nancheck=~all(isnan(reactionforce{3:end,2:end}),1);
reactionforce = reactionforce(2:end,nancheck);
time = table2array(reactionforce(2:end,1));
data = reactionforce(2:end,1:end);
data = table2array(data(1:end,1:end));
plot(time,data);
end
legend( sprintf('%g\n', vars) )
grid on;
axis tight;
xlabel('tijd [s]')
ylabel('reactiekracht [N]')
hold on;
end
YOu have to check your data in mat files then.
Jesse
Jesse on 9 Nov 2018
Edited: Jesse on 9 Nov 2018
Well, if i plot those separate, than you can see the code doens't work right
Can anyone help me?
Can you tell what exact is your problem?
Jesse
Jesse on 12 Nov 2018
Edited: Jesse on 12 Nov 2018
Well, there is a error in my code, it works, but not the results that i expect. It should subplot 2 different files, but that part doesn't work.
This is my current code;
clearvars
close all
clc
matfiles = dir('C:\Users\weikaj\Desktop\Jesse\7. Spoormodel\data\rf\*.mat') ;
for i = 1:length(matfiles)
filename = [matfiles(i).folder,filesep,matfiles(i).name] ;
load(filename) ;
for k = 1:i
vars = reactionforce{1,2:end};
cmap = colormap(parula(size(vars,2)));
subplot(2,1,k,'colororder',cmap); hold on
nancheck=~all(isnan(reactionforce{3:end,2:end}),1);
reactionforce = reactionforce(2:end,nancheck);
time = table2array(reactionforce(2:end,1));
data = reactionforce(2:end,1:end);
data = table2array(data(1:end,1:end));
%figure;
plot(time,data);
end
%legend( sprintf('%g\n', vars) )
grid on;
axis tight;
xlabel('tijd [s]')
ylabel('reactiekracht [N]')
hold on;
end

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Release

R2018a

Asked:

on 9 Nov 2018

Edited:

on 12 Nov 2018

Community Treasure Hunt

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

Start Hunting!