Effacer les filtres
Effacer les filtres

plot for loop results

1 vue (au cours des 30 derniers jours)
Dinara Ermakova
Dinara Ermakova le 18 Juil 2019
Commenté : Dinara Ermakova le 22 Juil 2019
Hi, MATLAB users
I am stuck on project where I have 15 subfolders with .txt data files in each of it. I need to plot results for each subfolder on same figure: go to folder 1, read files and extract vlies of one parameter, plot it and then move to the next subfolder.
Below is as far as I could go:
%% input files
input_dir = 'C:\Desktop\sensitivity\';
input_file = 'time.mat';
input_path = [input_dir input_file];
load(input_path); % time
ntimes = length(t);
Kdg = zeros(1,ntimes);
dir 'C:\Desktop\sensitivity\out\*\';
numfiles = 15;
mydata = cell(1, numfiles);
hold on
for k = 1:numfiles
% Kdm(i) = zeros(1,ntimes);
%Kd1g(i) = zeros(1ntimes);
%Kd1m(i) = zeros(1,ntimes);
for i=1:length(t)
filename = ['out' num2str(i) '.txt'];
mat=load([dir filename]);
sol = sum(mat(:,33:38),2)+ sum(mat(:,39:44),2);
aq = mat(:,20);
Kd = sol./aq;
Kdg(i,k) = geomean(sol)/(4*geomean(aq));
% Kd1\m (k) = mean(sol(1:169))/(mean(aq(1:168))*4);
% Kdg(k) = geomean(Kd1(1:169))/4;
% Kdm(k) = mean(Kd1(1:169))/4;
end
end
plot(t, Kdg)
set(gca, 'XScale', 'log');
ylabel('Kd (L/kg)');
xlabel('Time (sec)');
  1 commentaire
Dinara Ermakova
Dinara Ermakova le 18 Juil 2019
updated code a little but stll no luck
%% input files
input_dir = 'C:~\Desktop\sensitivity\';
input_file = 'time.mat';
input_path = [input_dir input_file];
load(input_path); % time
ntimes = length(t);
Kdg = zeros(1,ntimes);
dir 'C:~\Desktop\sensitivity\out\*\';
numfiles = 15;
mydata = cell(1, numfiles);
for k = 1:numfiles
input_dir2 = strcat('C:~\Desktop\sensitivity\out\', num2str(k), '\');
% Kdm(i) = zeros(1,ntimes);
%Kd1g(i) = zeros(1ntimes);
%Kd1m(i) = zeros(1,ntimes);
for i=1:length(t)
filename = ['out' num2str(i) '.txt'];
mat=load([input_dir2, filename]);
sol = sum(mat(:,33:38),2)+ sum(mat(:,39:44),2);
aq = mat(:,20);
Kd = sol./aq;
Kdg(i,k) = geomean(sol)/(4*geomean(aq));
% Kd1\m (k) = mean(sol(1:169))/(mean(aq(1:168))*4);
% Kdg(k) = geomean(Kd1(1:169))/4;
% Kdm(k) = mean(Kd1(1:169))/4;
end
plot(t, Kdg(:,k))
hold on
end

Connectez-vous pour commenter.

Réponse acceptée

Bob Thompson
Bob Thompson le 18 Juil 2019
Put your plot command inside the first loop, then index Kdg to only be for k elements.
for k = 1:numfiles
....
plot(t,Kdg(:,k)) % Not sure what t is, but I'm assuming it's universal for all plots
end
  3 commentaires
Bob Thompson
Bob Thompson le 18 Juil 2019
Is it the last line? If so, check that Kdg is appropriately saving all values.
Also, it shouldn't technically make a difference, but I would have left the 'hold on' line before both loops.
Dinara Ermakova
Dinara Ermakova le 22 Juil 2019
Finally, it worked.
Thank you

Connectez-vous pour commenter.

Plus de réponses (0)

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!

Translated by