-1

I have a script, which I need to interact with both via command line and UI interaction through some figures.

How do I programmatically switch the focus between console and full screen figure?

If I do figure() then a new figures is open in front of Matlab, but it does not have the focus. I have not idea how to do the opposite...

Wolfie
  • 27,562
  • 7
  • 28
  • 55
00__00__00
  • 4,834
  • 9
  • 41
  • 89
  • 2
    See [this answer](https://www.mathworks.com/matlabcentral/answers/88176-keep-focus-in-command-window#answer_97749), and the function [`commandwindow`](https://www.mathworks.com/help/matlab/ref/commandwindow.html). – Dev-iL Nov 09 '17 at 16:02

2 Answers2

1

This is not a clean solution, but a workaround:

% Create figure
testFigure = figure();

pause(3);

% Hide
testFigure.Visible='off';

pause(3);

% Bring to front
testFigure.Visible='off';
testFigure.Visible='on';

Works for me on R2017b.

S. Meier
  • 51
  • 2
1

When creating a figure, assign it to a handle

myFig1 = figure;
myFig2 = figure;

Then switch using

figure(myFig1); % Switches to myFig1
figure(myFig2); % Switches to myFig2

You can do the same with figure indices, but that is less clear and more prone to errors, for instance if you close/open other figures before switching

figure(1); % Initialise and/or switch to figure 1

Both of those methods will switch the active window to be the relevant figure window. To switch the active figure but keep the active window as the main Matlab editor, use set

set(0, 'CurrentFigure', myFig1) 
Wolfie
  • 27,562
  • 7
  • 28
  • 55
  • You might want to include a mention of `gcf` and similar functions to get the figure handle for existing figures. – Matt Nov 09 '17 at 17:00
  • If the OP is looking to switch between figures, it's likely they're not interested in the current one? [For other readers, `gcf` gets the current figure] – Wolfie Nov 09 '17 at 17:01