Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Cell mode and focus

Subject: Cell mode and focus

From: Jochen Deibele

Date: 23 Sep, 2010 08:27:19

Message: 1 of 5

Hi!

For testing scripts I like the cell mode in Matlab.
Now, if I'm executing a cell which opens a figure, the editor looses
focus and the figure is focused. Pressing a key I return to the Matlab
command prompt, not the editor. My editor is usually docked in the main
window.

My questions are:
- Is there a way to open figures without focusing them?
- Is there a way to programatically focus the main window?

The current workaround is using alt-tab to come back to matlab, but if I
open several figures I have to alt-tab-tab-tab-tab ... so it gets
cumbersome.

Any ideas are welcome!

(@Mathworks: what about interpreting the Ctrl-up / Ctrl-down /
Ctrl-enter commands in the figure window to highlight and or execute the
current / next cell?)


I have considered:
* visible off
   -> no option, I'm interested in what's plotted in the window
* focus editor from Yair Altman [1]
   desktop = com.mathworks.mde.desk.MLDesktop.getInstance;
   jEditor = desktop.getGroupContainer('Editor').getTopLevelAncestor;
   jEditor.toFront()
   -> doesn't work (neither if undocked or docked?!)
* from this thread [2] i tried:
   mainFrame = com.mathworks.mde.desk.MLDesktop.getInstance.getMainFrame;
   mainFrame.toFront;
   to bring back the focus to the main matlab window.
   -> The window bar of the figure flashes, but finally the figure has
   the focus.
* also from [2]:
   uimenufcn(0, 'WindowCommandWindow');
   -> doesn't work

Oh yes, I'm running 2010a on WinXP32 if that's of any use.

Thanks!
Jochen

[1] http://undocumentedmatlab.com/blog/accessing-the-matlab-editor/
[2] http://www.mathworks.cn/matlabcentral/newsreader/view_thread/269716

Subject: Cell mode and focus

From: Robert Cumming

Date: 23 Sep, 2010 09:23:09

Message: 2 of 5

Jochen Deibele <jmdeibele@gmx.de> wrote in message <i7f317$29t$1@orkan.itea.ntnu.no>...
> Hi!
>
> For testing scripts I like the cell mode in Matlab.
> Now, if I'm executing a cell which opens a figure, the editor looses
> focus and the figure is focused. Pressing a key I return to the Matlab
> command prompt, not the editor. My editor is usually docked in the main
> window.
>
> My questions are:
> - Is there a way to open figures without focusing them?
> - Is there a way to programatically focus the main window?
>
> The current workaround is using alt-tab to come back to matlab, but if I
> open several figures I have to alt-tab-tab-tab-tab ... so it gets
> cumbersome.
>
> Any ideas are welcome!
>
> (@Mathworks: what about interpreting the Ctrl-up / Ctrl-down /
> Ctrl-enter commands in the figure window to highlight and or execute the
> current / next cell?)
>
>
> I have considered:
> * visible off
> -> no option, I'm interested in what's plotted in the window
> * focus editor from Yair Altman [1]
> desktop = com.mathworks.mde.desk.MLDesktop.getInstance;
> jEditor = desktop.getGroupContainer('Editor').getTopLevelAncestor;
> jEditor.toFront()
> -> doesn't work (neither if undocked or docked?!)
> * from this thread [2] i tried:
> mainFrame = com.mathworks.mde.desk.MLDesktop.getInstance.getMainFrame;
> mainFrame.toFront;
> to bring back the focus to the main matlab window.
> -> The window bar of the figure flashes, but finally the figure has
> the focus.
> * also from [2]:
> uimenufcn(0, 'WindowCommandWindow');
> -> doesn't work
>
> Oh yes, I'm running 2010a on WinXP32 if that's of any use.
>
> Thanks!
> Jochen
>
> [1] http://undocumentedmatlab.com/blog/accessing-the-matlab-editor/
> [2] http://www.mathworks.cn/matlabcentral/newsreader/view_thread/269716

This doesn't answer either of your questions but might be a suitable workaround:

If you have the figure(s) already created then using:

cla (axes)
plot ( axes, x, y .... )

will stop the focus going to the figures (but you will need to have all the figures already created... (or you could use subplot)
---------------
Another way is to add a callback to the KeyPressFcn which will close your figure:

figure ( 'KeyPressFcn', 'close' );
plot ( [1 2 3], rand.*[2 3 4] )

That way the focus will return to the last known focus (i.e. the editor)

Subject: Cell mode and focus

From: Jochen Deibele

Date: 23 Sep, 2010 10:47:52

Message: 3 of 5

Hi!


Robert Cumming wrote on 23.09.2010 11:23:

> This doesn't answer either of your questions but might be a suitable workaround:
>

Thanks for thinking about it - also a good workaround can solve problems.

  
> If you have the figure(s) already created then using:
>
> cla (axes)
> plot ( axes, x, y .... )
>

I thought of that also. However, then I have to keep track of the axes
handles for all of my plots. Which is possible, but if I don't have to,
I want to avoid the programming overhead.

 > will stop the focus going to the figures (but you will need to have
 > all the figures already created... (or you could use subplot)

 > ---------------

> Another way is to add a callback to the KeyPressFcn which will close your figure:
>
> figure ( 'KeyPressFcn', 'close' );
> plot ( [1 2 3], rand.*[2 3 4] )
>
> That way the focus will return to the last known focus (i.e. the editor)

I didn't think of that one and it suits in some cases. However, often I
just want to continue with the next cell ignoring the windows - but not
closing them, as I could need them later on.

Thanks!

Jochen

Subject: Cell mode and focus

From: Robert Cumming

Date: 23 Sep, 2010 14:11:19

Message: 4 of 5

Jochen Deibele <jmdeibele@gmx.de> wrote in message <i7fb8o$ajs$1@orkan.itea.ntnu.no>...
> Hi!
>
** snip **

I understand your point in the overhead of axes/figures etc....

You could expand the second method by:

function gui_Test
  figure ( 'KeyPressFcn', {@CloseMeOrHideMe} );
  plot ( [1 2 3], rand.*[2 3 4] )
  figure ( 'KeyPressFcn', {@CloseMeOrHideMe} );
  plot ( [1 2 3], rand.*[2 3 4] )
  figure ( 'KeyPressFcn', {@CloseMeOrHideMe} );
  plot ( [1 2 3], rand.*[2 3 4] )
end
function CloseMeOrHideMe ( obj, ~ )
  switch get (obj, 'CurrentKey' )
    case 'v'
      set ( obj, 'visible', 'off' )
    otherwise
      close ( obj );
  end
end

%%%
Ofcourse you will have to turn back on your figures later, you can do this by "figure(#)" and it will either re-visualise your plot or create an empty figure.

Subject: Cell mode and focus

From: Yair Altman

Date: 23 Sep, 2010 16:54:05

Message: 5 of 5

Jochen Deibele <jmdeibele@gmx.de> wrote in message <i7f317$29t$1@orkan.itea.ntnu.no>...
> Hi!
>
> For testing scripts I like the cell mode in Matlab.
> Now, if I'm executing a cell which opens a figure, the editor looses
> focus and the figure is focused. Pressing a key I return to the Matlab
> command prompt, not the editor. My editor is usually docked in the main
> window.
>
> My questions are:
> - Is there a way to open figures without focusing them?
> - Is there a way to programatically focus the main window?
>
> The current workaround is using alt-tab to come back to matlab, but if I
> open several figures I have to alt-tab-tab-tab-tab ... so it gets
> cumbersome.
>
> Any ideas are welcome!
>
> (@Mathworks: what about interpreting the Ctrl-up / Ctrl-down /
> Ctrl-enter commands in the figure window to highlight and or execute the
> current / next cell?)
>
>
> I have considered:
> * visible off
> -> no option, I'm interested in what's plotted in the window
> * focus editor from Yair Altman [1]
> desktop = com.mathworks.mde.desk.MLDesktop.getInstance;
> jEditor = desktop.getGroupContainer('Editor').getTopLevelAncestor;
> jEditor.toFront()
> -> doesn't work (neither if undocked or docked?!)
> * from this thread [2] i tried:
> mainFrame = com.mathworks.mde.desk.MLDesktop.getInstance.getMainFrame;
> mainFrame.toFront;
> to bring back the focus to the main matlab window.
> -> The window bar of the figure flashes, but finally the figure has
> the focus.
> * also from [2]:
> uimenufcn(0, 'WindowCommandWindow');
> -> doesn't work
>
> Oh yes, I'm running 2010a on WinXP32 if that's of any use.
>
> Thanks!
> Jochen
>
> [1] http://undocumentedmatlab.com/blog/accessing-the-matlab-editor/
> [2] http://www.mathworks.cn/matlabcentral/newsreader/view_thread/269716


two other alternatives come to my mind:

1) minimize your newly-created figure windows - there are a couple of utilities on the File Exchange for this, or you can search the CSSM newsgroup (there were several posts about this in each of the past few years). Because the figures are minimized, you should get the latest window (=Editor/Main) by definition.

2) Use Java's Robot class to automate the Alt-Tab keystroke upon each figure creation - I posted about this last week: http://undocumentedmatlab.com/blog/gui-automation-robot/

Yair Altman
http://UndocumentedMatlab.com

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us