MATLAB Answers

How to recall a figure which was generated by a name "fname = figure; figure(fname)" without saving it ?

5 views (last 30 days)
mary on 28 May 2020
Edited: Fangjun Jiang on 10 Jun 2020
I have many figures and for validation purpouse, I have a file1.m file which generate many figures. I clean the worskspace without closing figures and I run file2.m. I want to have the results of file2 to be put on those of file1. This is straightforward when you use figure(1),...,figure(100). However, I have many figures and rather than remembering the correspondance between the variables to be plot and the figure numbers, I want to have something like this:
fpressure = figure; figure(fpressure);
ftemperature = figure;figure(ftemperature)
This works perfectly within one file. As soon as I use clear command, it is forgotten which figure was fpressure and ftemperature. So the file2's plots wont be on those have been generated before. Is there any mean to do what I want to do ?


Sign in to comment.

Answers (1)

Fangjun Jiang
Fangjun Jiang on 28 May 2020
Edited: Fangjun Jiang on 28 May 2020
When you run fpressure=figure, it creates the figure object handle fpressure. But after you clear the workspace, the figure object handle is gone although the figure window still exists. You can "find" those figure obejct handles but won't be able to match by figure object handle name. I suggest you add some keywords to be able to find it later.
close all; clear all;
close all; clear all;


Show 2 older comments
Fangjun Jiang
Fangjun Jiang on 29 May 2020
In the first solution, only two figures are created. The key is to use findall() in your file2.m to retrive the figures that were created in your file1.m. But you need to provide some keywords when creating the figures to be able to find a match.
In the second solution, by assigning a figure number (it could be any number as long as they are the same between file1.m and file2.m), you don't need to use findall().
Fangjun Jiang
Fangjun Jiang on 10 Jun 2020
Please read it again. In the first section, I was trying to explain that you could find the figure but couldn't tell which one is which because there is no keyword (or anything to identify the figure).
In the second section, you can find and match.
If you ran both section of the code, there are two figures left. The two created in the first section were closed by "close all".
The solution is in the second section, if you could understand it.

Sign in to comment.