Filter löschen
Filter löschen

plot for loop results

1 Ansicht (letzte 30 Tage)
Dinara Ermakova
Dinara Ermakova am 18 Jul. 2019
Kommentiert: Dinara Ermakova am 22 Jul. 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 Kommentar
Dinara Ermakova
Dinara Ermakova am 18 Jul. 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

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Bob Thompson
Bob Thompson am 18 Jul. 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 Kommentare
Bob Thompson
Bob Thompson am 18 Jul. 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 am 22 Jul. 2019
Finally, it worked.
Thank you

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Line Plots finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by