views:

325

answers:

3

I'm using the C++ API to fire up MATLAB (via engOpenSingleUse). Everything's working fine. But I'd like to change the title of the window from "MATLAB Command Window" to something else.

I often have 4 or 5 of them open, and occasionally one gets orphaned if my program crashes. If I could change the title, I'd have a better shot of knowing which one was which.

Is there a MATLAB command I could execute (via engEvalString) that would do this?

A: 

You can use a shareware tool such as the EPC Title Bar Changer to do this.

0A0D
eh, I want it to be a MATLAB command.
Eric H.
Probably out of luck since it is likely hard-coded
0A0D
+6  A: 

For Matlab 7:

jDesktop = com.mathworks.mde.desk.MLDesktop.getInstance;
jDesktop.getMainFrame.setTitle('my new title');

*or specifically for the Command Window:

cmdWin = jDesktop.getClient('Command Window');
cmdWin.getTopLevelAncestor.setTitle('my new title');

For Matlab 6:

jDesktop = com.mathworks.ide.desktop.MLDesktop.getMLDesktop;
jDesktop.getMainFrame.setTitle('my new title');

*or for the Command Window:

cmdWin = jDesktop.getClient('Command Window');
cmdWin.getTopLevelWindow.setTitle('my new title');


Other related undocumented desktop features are described here:
http://UndocumentedMatlab.com/blog/tag/desktop/

Yair Altman
Darn.That works in a MATLAB I start myself. (The first two lines, using getMainFrame). It does not work in a MATLAB Command Window started with engOpen ().I can type commands there, here's the error:» jDesktop = com.mathworks.mde.desk.MLDesktop.getInstance;» cmdWin = jDesktop.getClient('MATLAB Command Window');» cmdWin.getTopLevelAncestor.setTitle('my new title');??? Attempt to reference field of non-structure array.
Eric H.
Eric - the problem is probably because the code correctly gets the jDesktop handle but returns an empty cmdWin handle. This is probably because you don't have an open window named "MATLAB Command Window" when you try to get the cmdWin reference handle. Try running jDesktop.getClientTitles (or similar variants - see methods(jDesktop) for a full list of supported methods) to get the actual name of the desktop window client.
Yair Altman
Hmmm..... Here's what I see..... getClientTitles returns 9 entries, 'Command Window', 'Command History', etc etc etc. But I only see one window on my windows desktop, and it is called 'MATLAB Command Window'. When I call jDesktop.getClient with either string ('MATLAB Command Window' or 'Command Window') it just returns the empty set []. I do not have a regular instance of MATLAB running, this is the instance I have from engOpen (). Thanks for trying to help, I really appreciate it!
Eric H.
Eric - perhaps you are trying to retrieve the client handle too soon after calling engOpen()? - the Command Window client might still not be ready when you call getClient(). Perhaps try inserting a short delay pause()?
Yair Altman
Yair - no, once the Command window opens, I can either use the endEvalString() functions, *OR* I can type commands directly into the Commadn window myself. Neither one works, so it's not a timing problem. My guess is that the Command Window is somehow different than a regular "full" MATLAB as far as the java UI classes go. Sigh.
Eric H.
then I guess you're stuck with: com.mathworks.mde.desk.MLDesktop.getInstance.getMainFrame.setTitle('my new title');
Yair Altman
I don't understand what you mean by "stuck with", that doesn't work either. Again, I really appreciate you're trying to help, but I guess this just won't work for the windows engOpened versions.
Eric H.
Oh well.... I guess there's no good solution. I ended up just putting a string variable into the instances namespace via engEvalString(), which I can query using who.Thanks for trying, though Yair... your solution is the based I've seen.
Eric H.
A: 

Try coding directly against the Java AWT classes. This may be more flexible and work inside the Matlab engine running under C++. (Haven't tested it in that context, since I don't use the engine.)

function change_win_title(oldName, newName)

wins = java.awt.Window.getOwnerlessWindows();
for i = 1:numel(wins)
    if isequal(char(wins(i).getTitle()), oldName)
        wins(i).setTitle(newName);
    end
end

You'd use it like this.

change_win_title('MATLAB Command Window', 'My new window name')

You can use other tests (window class, etc) to identify the windows of interest.

Andrew Janke
works in the regular GUI, does not work in the Engine command window
Eric H.