plot for loop results

1 回表示 (過去 30 日間)
Dinara Ermakova
Dinara Ermakova 2019 年 7 月 18 日
コメント済み: Dinara Ermakova 2019 年 7 月 22 日
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 件のコメント
Dinara Ermakova
Dinara Ermakova 2019 年 7 月 18 日
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

サインインしてコメントする。

採用された回答

Bob Thompson
Bob Thompson 2019 年 7 月 18 日
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 件のコメント
Bob Thompson
Bob Thompson 2019 年 7 月 18 日
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 2019 年 7 月 22 日
Finally, it worked.
Thank you

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLine Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by