MATLAB Answers

How to stop figure windows from popping up when there is no data to plot.

조회 수: 18(최근 30일)
Tanner Avery
Tanner Avery 16 Mar 2021
답변: Jeff Miller 16 Mar 2021
I have a code that creates figures using multiple loops. There are conditional statements within my code that dictate whether a plot is created or not based on certian conditions. My problem is that even when the conditions are such that no plot is created an empty figure window still pops up as if a plot was created. This is leading to a lot of clutter as my program is outputting dozens of figure windows when only 2-3 are needed. Below is an example of my problem. When h = 0 the program outputs a plot for Figure 2 and a blank window for figure 1 (see attached photo). What method could I use to not have the window for figure 1 appear at all when h=0?
vmin=1;
vmax = 20;
h = 0;
f1=figure('Name','Figure x');
f2=figure('Name','Figure y');
while vmin < vmax
v = vmin;
x=v;
y=v/2;
if h == 1
set(0,'CurrentFigure',f1);
hold on
plot(x,y,'or');
title ('figure 1')
hold off
end
y2=v*2;
set(0,'CurrentFigure',f2);
hold on
plot(x,y2,'ok');
title ('figure 2')
hold off
vmin=vmin+1;
end

채택된 답변

Jeff Miller
Jeff Miller 16 Mar 2021
The commands
f1=figure('Name','Figure x');
f2=figure('Name','Figure y');
make the two windows pop up, so you must change your code so that those commands are only executed in cases where you actually want the windows. Not sure what you intend, but it looks like maybe you want the 'figure' commands inside the 'if' statement.

추가 답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by