how to set figure handles in a for loop?

조회 수: 8 (최근 30일)
Yasmin Samy
Yasmin Samy 2018년 1월 9일
답변: Erin Richards 2022년 12월 14일
Hello! I have a for loop that uses two different tables to simultaneously create two figures (multiple curves are plotted on each figure with each iteration - so we use hold on). hold on is that it continues plotting on the same figure (multiple curves - multiple year time series).
It worked with one of my for loops but not on the first. I`m not sure what i`m missing. Below is an extract of the for loop...
%defining data sets
py_yr =PSD_avg(k,4:25);
py_stdyr=PSD_std(k,4:25);
figure(figa)
plotpsd=semilogx(psd_x,py_yr,'-o','Color',col);
title({['PSD at ',stn,' (', yrtitle,')']});
set(gca,'Fontsize',12);
ylabel('dV/dlnr');
hold on
figure(figb)
plotstd=semilogx(psd_x,py_stdyr,'-o','Color',col);
title({['Standard deviation at ',stn,' (', yrtitle,')']});
set(gca,'Fontsize',12);
ylabel('dV/dlnr');
hold on

답변 (1개)

Erin Richards
Erin Richards 2022년 12월 14일
Hi! If I understand correctly, I was able to make this work by setting figure handles before the for loop, toggling between the figures inside the for loop, and using hold on to plot data on the same figure in each loop.
% SET THE FIGURE HANDLES.
f1 = figure;
f2 = figure;
% SET UP SOME STUFF OUTSIDE THE FOR LOOP.
% Put in your x values here. I just made a list of logarithmic values.
psd_x = [1, 10, 100, 1000];
% Put in your y values here. I just made a matrix of random numbers less than 100.
PSD_avg = rand(100,10,4);
% Put in your y error here. I just made a matrix of random numbers less than 10.
PSD_std = rand(10,10,4);
% START THE FOR LOOP.
for k = 1:10 % Put in how many iterations you want here. I just used 10.
py_yr =PSD_avg(k,1:4); % I'm using the first four columns. You should change this to fit yours.
py_stdyr=PSD_std(k,1:4);
% Plot stuff on the first graph.
set(0, 'CurrentFigure', f1) % Select the first graph
hold on % To keep all of the current data on the graph.
plotpsd=semilogx(psd_x,py_yr,'-o'); % Plot the stuff.
% Plot stuff on the second graph.
set(0, 'CurrentFigure', f2)
hold on
plotstd=semilogx(psd_x,py_stdyr,'-o');
end
% MAKE THE GRAPHS LOOK NICE.
% Make the first graph look good.
set(0, 'CurrentFigure', f1)
title('PSD at year');
set(gca,'Fontsize',12);
ylabel('dV/dlnr');
hold off % Finish the first graph.
%Make the second graph look good.
set(0, 'CurrentFigure', f2)
title('Standard deviation at year');
set(gca,'Fontsize',12);
ylabel('dV/dlnr');
hold off % Finish the second graph.
I wasn't totally sure what you were trying to do with the titles, but I'm sure you can figure that part out!
Hope this helps! Good luck!

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by