Load different files in a loop
Show older comments
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.
Answers (1)
KSSV
on 9 Nov 2018
matfiles = dir('*.mat') ;
for i = 1:length(matfiles) ;
filename = matfiles(i).name ;
load(filename) ;
% do what you want
end
14 Comments
Jesse
on 9 Nov 2018
Jesse
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
KSSV
on 9 Nov 2018
Have a look on plot/ subplot.
Jesse
on 9 Nov 2018
KSSV
on 9 Nov 2018
Check the files...they should differ in name.
Jesse
on 9 Nov 2018
KSSV
on 9 Nov 2018
YOu have to check your data in mat files then.
Jesse
on 10 Nov 2018
KSSV
on 12 Nov 2018
Can you tell what exact is your problem?
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


