how to set figure handles in a for loop?
24 views (last 30 days)
Show older comments
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
0 Comments
Answers (1)
Erin Richards
on 14 Dec 2022
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!
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!