How to determine the figure window on which subplots are created?

3 views (last 30 days)
hmhuang
hmhuang on 30 Nov 2021
Edited: hmhuang on 30 Nov 2021
I have two figure windows, say, f1 = figure(1) and f2 = figure(2). For f1, it's only plotted once at the beginning and I want to keep the content of this figure window intact till the end of the program. For f2, there are several subplots created on this figure window and updated for each iteration. However, if I clicked f1 when the code is running, all stuff supposed to be on f2 will be shown on f1 and the original content of f1 is overlapped....
My question is: How to specify a parent figure window where subplot is created, so that I can makes sure the suplots are consistently created on f2 throughout the program?

Accepted Answer

dpb
dpb on 30 Nov 2021
subplot() doesn't allow a figure handle; use the alternate meaning of figure() to set the figure before calling subplot()
figure(f2) % this presumes there is a typo in the Q? above and fs=figure(2); was intended
subplot(...)
...
The above alone does not prevent someone from changing focus manually if a piece of code is running and the user clicks on the other figure. Having the two lines of code together on a given line I think makes them atomic--
figure(f2); hAx(2)=subplot(...);
Now, from here forward always refer to the handle hAx(2) to ensure plotting commands are going to the right axes and things will stay straight regardless of which figure actually has focus.
You can also use gcf to retrieve what is the current figure and test it matches the desired figure handle and take evasive/corrective action as needed.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!