bayesopt
draws figures like this:
How to access such figures in order to modify title or something? If I use gcf
it is not guaranteed I get the correct figure because I could change focus to another figure window during execution.
bayesopt
draws figures like this:
How to access such figures in order to modify title or something? If I use gcf
it is not guaranteed I get the correct figure because I could change focus to another figure window during execution.
Apparently bayesopt
does not allow you to return a figure handle. So I suggest that on the line directly after your call to bayesopt
you call h=gcf;
, thus forcing your program to return the figure handle to h
, which can then be modified at any desired time, even when moving to other figures.
results = bayesopt(fun,vars,Name,Value); % execute bayesian optimisation
h = gcf; % directly after optimisation grab a figure handle
Now you can modify properties in h
, e.g. for the title you'd simply do
h.CurrentAxes.Title.String = 'Your plot title'
The reason this works is that MATLAB does not display figures until the full code has finished running. At least that's the case for my script, where I generate a figure, perform a few minutes of optimisation, then generate another figure. Both figures get displayed at the same time, i.e. when MATLAB has finished running the full program. There's thus no way you can click other figures when the code is running, since they are simply not there. If you happen to have older figures open (from other scripts), focus gets shifted to the latest created figure anyway, the moment it's created in the code (so not when it's displayed), thus you'd need to click a figure in the few milliseconds between the bayesopt
call finished and the gcf
call, which I'd say is so improbable that it's not worth considering, especially since it requires manual intervention.
As was pointed out in comments by Cris Luengo and Dev-iL, the figures are tagged and can thus be found using findobj
:
h1 = findobj(0,'tag','bayesopt.MinObjective')