0

bayesopt draws figures like this:

enter image description here

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.

Adriaan
  • 17,741
  • 7
  • 42
  • 75
Dims
  • 47,675
  • 117
  • 331
  • 600

1 Answers1

1

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')
Adriaan
  • 17,741
  • 7
  • 42
  • 75
  • `bayesopt` can run many hours (depending on task) and you always have time to switch figure before it ends; I made detecting figure in `plotFcn` callback, but this also doesn't guarantee result – Dims Sep 19 '18 at 13:46
  • 2
    @Dims: does the figure generated by `bayesopt` have a specific tag or other characteristic that you can search for? I'm thinking you could use `findobj` to find the right figure. – Cris Luengo Sep 19 '18 at 14:19
  • Probably it does, but I am not sure which tag to search and whether the figures from different runs have different tags... – Dims Sep 19 '18 at 14:25
  • 1
    @CrisLuengo yes - two figures are created by this function, one has the tag `'bayesopt.MinObjective'` and the other `'bayesopt.ObjectiveModel'`. @Dims The figures created later will appear first when you call `get(0,'Children')`. – Dev-iL Sep 19 '18 at 14:28
  • 1
    In that case, `h1 = findobj(0,'tag','bayesopt.MinObjective')` will return a handle to the first figure, and a similar line will find the other figure. (I don't have this toolbox, so cannot test, and therefore cannot write up this as an answer.) – Cris Luengo Sep 19 '18 at 14:29