Load different files in a loop
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
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.
Réponses (1)
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
Voir également
Catégories
En savoir plus sur Line Plots dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!