plot for loop results

1 view (last 30 days)
Dinara Ermakova
Dinara Ermakova on 18 Jul 2019
Commented: Dinara Ermakova on 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 Comment
Dinara Ermakova
Dinara Ermakova on 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

Sign in to comment.

Accepted Answer

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

Sign in to comment.

More Answers (0)

Categories

Find more on Line Plots 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!