Saving Specific Figure Handle For Recovery After Plotting Many Figures

22 views (last 30 days)
I'm writing quite a complicated simulation which involves multiple calls to different functions/subfunctions. The full setup has >100 switch cases, which usually all run and produce multiple plots. The overall intention is to get the code to switch to the 'best figure' (as set by some error metric we're minimising) once a function has finished running.
As a simple functioning piece of code which should cover my requirements whilst maintaining SOME simplicity, think of a structure something like this will suffice:
%Main Script
x = 0:0.01:10; Answers = function_1(x);
%% Function Files:
function Answers = function_1(x)
for methods = 1:2
switch methods
case 1; ph = 1; %Do Stuff
case 2; ph = 2; % Do Other Stuff
end
[Err,~] = function_2(x,ph); %%Take Results from Switches Make some other calculation and a PATCH plot
%*************************************************** Error Check Section - Save Axes/Figure No. Here from fun_2?
if Err(end) <= Err_min
Err_min = Err(end);
%Would Like to Save the figure handle here in a way such I can command line switch to that specific,
% most recent figure when there are a few 100 open.
end
%***************************************************
Answers = Err;
end
%Additional Line Probably Needed Here To Switch To/Open The Figure Saved In The Error Check
%May include a save command here also..
end
function [Err,subresults] = function_2(x,ph)
y = x.*ph;
custom_plot_function(x,y); %Basically just a complicated ****patch object**** with titles & axes and markers & stuff in my actual code
subresults = x.^2 .*y;
Err = abs(y-subresults);
end
From the documentation, I think fig = gcf; ax = fig.CurrentAxes will get me the current axes handle, but I'm unsure how to use that handle to find the specific figure from the 100's generated, I have a feeling this is a simple syntax issue but I just can't find it in the documentation.

Accepted Answer

Matt J
Matt J on 3 Nov 2020
When you open a figure or axis you can save a handle to it, e.g.,
h(1)=figure(1);
h(2)=figure(2);
...
You can later use whichever h(i) you need to manipulate the figure.
  3 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by