Borrar filtros
Borrar filtros

plot for loop results

1 visualización (últimos 30 días)
Dinara Ermakova
Dinara Ermakova el 18 de Jul. de 2019
Comentada: Dinara Ermakova el 22 de Jul. de 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 comentario
Dinara Ermakova
Dinara Ermakova el 18 de Jul. de 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

Iniciar sesión para comentar.

Respuesta aceptada

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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Line Plots en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by