Load different files in a loop

6 vues (au cours des 30 derniers jours)
Jesse
Jesse le 9 Nov 2018
Modifié(e) : Jesse le 12 Nov 2018
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 commentaires
Stephen23
Stephen23 le 12 Nov 2018
Modifié(e) : Stephen23 le 12 Nov 2018
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 le 12 Nov 2018
Modifié(e) : Jesse le 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

Connectez-vous pour commenter.

Réponses (1)

KSSV
KSSV le 9 Nov 2018
matfiles = dir('*.mat') ;
for i = 1:length(matfiles) ;
filename = matfiles(i).name ;
load(filename) ;
% do what you want
end
  14 commentaires
KSSV
KSSV le 12 Nov 2018
Can you tell what exact is your problem?
Jesse
Jesse le 12 Nov 2018
Modifié(e) : Jesse le 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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Line Plots dans Help Center et File Exchange

Produits


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by