Run a function after keypress in MATLAB - matlab

I want MATLAB to run a function after I press a specific key "s" from the keyboard while I am within the MATLAB application (focused in MATLAB window). Is there a way to do that without the use of any external packages or libraries?
Thank you.

You can set custom shortcuts in Matlab by selecting:
Home > Preferences > Shortcuts
The closest option to what you want is "evaluate entire file" which runs the current script / function file open in the editor. I don't know of any ability to run a specific function using this, but you can assign the shortcut "S" as shown below.

Related

Using Enter as Accelerator in MATLAB UIMenu

I'm building a GUI in MATLAB and currently adding custom menus using uimenu. I'm trying to add different accelerators to different menu actions.
I've found that passing char(10) (newline) as the accelerator character in uimenu (see below) matlab adds Ctrl+ Enter as that menu's accelerator label. The problem is that it will not run the callback when I hit Ctrl+ Enter.
Any ideas why this doesn't work? Am I missing something? Is the Ctrl+ Enter for "run current section" cancelling my call? In that case, can I override it?
Example
A quick demonstrative example of how MATLAB will not accept Ctrl+ Enter
function test
close all
f=figure;
m=uimenu(f,'Label','test');
uimenu(m,'Label','a','callback',#hittest,'Accelerator','r');
uimenu(m,'Label','b','callback',#hittest,'Accelerator',char(10));
function hittest(h,~)
disp(h.Label)
end
end
As you've stated, it appears the main application has registered this accelerator and is therefore preventing your GUI from intercepting this call.
You could try to change MATLAB's keyboard shortcut in the shortcut preferences dialog. Note that this will only affect your installation of MATLAB.
If you start MATLAB in -nodesktop mode, then this will prevent the MATLAB IDE from launching the IDE and should free up the accelerator for your use.
matlab -nodesktop
Since you mention that this will be a deployed application, you could always use isdeployed to check if it is being run as a deployed application and if it's not then you could use an alternate keyboard shortcut so you don't have to continuously start MATLAB without an IDE
if ~isdeployed
% Use some other keyboard shortcut for testing
set(hmenu, 'Accelerator', <some other key for testing>)
else
% Use the enter key on deployed applications
set(hmenu, 'Accelerator', char(10))
end
You could also make it so that any time your app is deployed or matlab is being run with -nodesktop it would use the enter key:
if usejava('desktop')
% Use some other keyboard shortcut for testing
set(hmenu, 'Accelerator', <some other key for testing>)
else
% Use the enter key on deployed applications
set(hmenu, 'Accelerator', char(10))
end

Matlab control+enter key on figure

I want to capture when the user holds down the control key and presses the enter key on a figure window. Note: This is the default keys for "Evaluate Current Section" in the editor.
See example code below:
function test
f = figure;
f.KeyPressFcn = #myKeyPressFcn;
end
function myKeyPressFcn ( hFig, event )
cm = hFig.CurrentModifier();
if ~isempty ( cm )
fprintf ( 'CurrentKey: %s:%s\n', cm{1}, hFig.CurrentKey );
else
fprintf ( 'CurrentKey: %s\n', hFig.CurrentKey );
end
end
To reproduce save the above in an active file in the editor and run the function - the editor needs to be open (this is important!!).
With the figure active press any key -> the selected key is written to the terminal window. However if you hold down Control and press the enter (return) key then this is not captured but instead Matlab attempts to "Evaluate Current Section" (or cell as it used to be called) in the editor (no matter that the figure has the focus). This of course throws as error...
I have contacted Matlab support and this is "expected behaviour!". I can (just about) see why it might be a good idea for demos - but for professional applications that run in Matlab I personally think this "feature" is a bug!!
Notes
When the editor is closed the control+enter is captured in the figure
In deployed applications the control+enter is captured.
If I manually change the Evaluate Current Section shortcut then control+enter is captured.
I have tried a number of things to resolve this but none have worked, for example hiding the editor or setting editor enable state to false (neither of these are acceptable solutions - I was trying to see what I could get to work on a small test case...):
function test
desktop = com.mathworks.mde.desk.MLDesktop.getInstance;
jEditor = desktop.getGroupContainer('Editor').getTopLevelAncestor;
jEditor.setVisible(false);
jEditor.setEnable(false);
f = figure
f.KeyPressFcn = #myKeyPressFcn;
uiwait(f);
jEditor.setVisible(true);
jEditor.setEnable(true);
end
The only way I can get it to work is to close all of the editor files on launching the GUI and then opening them again when the GUI closes (this is not an acceptable solution... - for fairly obvious reasons!!)
I did think about trying to temporarily modify the shortcut key (Preferences-Keyboard-Shortcuts) of the "Evaluate Current Section" -> but haven't worked out a way to do it from the commandline, and then set it back again when finished. If this is fast you could do it when the user presses and releases the control key.
So what am I asking:
If possible I need a solution that will work for anyone anywhere - as if I can get this to work it will be included in a new add-on feature in my Matlab GUI Toolbox. - which is used by people all over the world.
Do you know how to modify the keyboard shortcuts from the commandline - if so how!
Any other suggestions?
My other idea is to change my shortcut to a different key combination - but wheres the fun in that! :) (I will still have the issue if some user somewhere has altered the execute the current cell to my new combination...)
P.S. Thanks for reading this far down!! :)
Why don't you go to the home> Preferences > keyboard > Shortcutand change it there?
you only need to hit Ctrl + Enter in the black box at top of the page for searching the related command, which is here Evaluate Current Section and change it whatever you like.
Please bear in mind you will only need to split out your windows (Undock them). Then, when you click on Ctrl + Enter, it will do whatever you would like.
I hope you find this answer helpful.
You can try the solution from my FEX submission. The KbTimer is motivated by the need to capture keyboard stroke without the need of GUI that designed either by GUIDE or APP DESIGNER. Note that the implementation of function was inspired from the PsychToolbox which is MEX based.

matlab - can I view functions run by plugins?

I am using matlab R2012b, with the eeglab plugin. This is a plugin with a GUI, while this is very helpful I want to be able to see what functions the plugin is running when I use the GUI is this possible?
If you enter the command EEG.history into the command window after your session with EEGLAB it will print out a list of the commands called for the session.
example ...
EEG.history
ans =
EEG = pop_fileio('D:\work\Matlab libraries\training_course_materials\Eeglab_data.set');
EEG.setname='temp_file';
EEG = eeg_checkset( EEG );
If you then select a function of interest, e.g. highlight pop_fileio above and then right click you can select 'Open selection' option from the popup menu and the file pop_fileio.m' will open in a new tab in the MATLAB editor for you to look at it.
It's worth noting that when you run EEGLAB as a GUI that a lot of subsequent function calls are made with default parameter setting however if you call them directly from your own code you can then change these default settings.
You can follow the execution of a program using debugger. It is very easy using matlab's editor. Alternatively you can use profiler to see what was exectured and how many times.
When you select an option on the GUI, a new form/window will open. The name of the fucntion will be displayed at the top of this new window. Locate where you have stored EEG lab and find the function pertaining to the name and you can view the details in your editor window.

Configure pointer focus properties of Matlab's command window

I'm running Matlab 2013a, under Linux, using Xmonad (using the XMonad.Config.Xfce package).
This problem occurs whether the command window is docked or not.
The command window prompt does not get the keyboard focus unless the pointer is located
over the command window.
Is there a way to get the Matlab command window to have focus behaviour just like other normal windows, like a terminal?
Most important: I'd like to have the keyboard focus follow the window focus,
and not require any special positioning of the pointer, so that I can just "Alt-Tab" around my windows and have the command window get the keyboard focus. All of the resources I've found so far relate to programmatic control of focus; I'm just trying to improve my user experience in an interactive session.
To get keyboard focus on the Command Window, include the following in your xmonad.hs
import XMonad.Hooks.SetWMName
import XMonad.Hooks.ManageHelpers
and configure your ManageHook as follows
myManageHook = composeAll . concat $
[ [appName =? a --> doCenterFloat | a <- myFloatAS ]
, (your other hooks)
] where
myFloatAS = ["MATLAB"]
Finally, include setWMName "LG3D" in your startupHook. See here for a full xmonad.hs configuration which uses this (this is where I found the solution). If you have other Java apps that don't get focus as they should you can add them to the myFloatAS list.
It's a problem in the built-in java.
If i run:
export MATLAB_JAVA=/usr/lib/jvm/java-7-openjdk/jre
matlab -desktop
Matlab works as expected.
I ran into this problem, running MATLAB2014a. I set up setWMName "LG3D" but still i couldn't get focus on my window. I had to click on the focused window to get the cursor, and sometimes the situation was even worse and I had to click on random places till i get my cursor back. This wouldn't happen on MATLAB2010. What worked for me was to use the native version of java as describe above.
In the end, i used the following bash script to start matlab8:
#!/bin/bash
export MATLAB_JAVA=/usr/lib/jvm/java-7-openjdk-amd64/jre/
/usr/local/bin/matlab8 -desktop -nosplash

(matlab)implement "Evaluate or Open Code You Select" in everywhere? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do you retrieve the selected text in MATLAB?
I want to implement and add some features to the function "Evaluate Selections", where you can highlight code and then "Evaluate Selections" by right click your mouse (or F9).
In the editor environment, this is how it is done:
editorObject = matlab.desktop.editor.getActive;
eval([editorObject.SelectedText ';']);
How can I implement this from the command line window, or the help window?
EDIT:
Maybe I didn't express my question clearly.
Imagining that we already have this function called eva_select(), I can use function this way:
I wrap the function as the Shortcuts button.
Use mouse to select a variable at command line window, maybe I entered before, say var_a
Then I click that Shortcuts button, the text which I selected before will be executed. This is exactly as press F9 key or choose right mouse menu -- "Evaluate Selections".
But if we really have that function, we can do more! We can modified eva_select() to eva_select_size(), in this way, we can select a variable, say var_a at command line window or help window, click eva_select_size() shortcuts button, then, we will get size(var_a) at command line window!
EDIT:
Thanks, I can retrieve the text in the command window, but I can't do the same thing in the help window, is it possible to do that?
The command window, like other GUI components in the MATLAB desktop, is Java-based. Therefore it can be accessed programmatically, but it is completely undocumented and its use is not officially supported.
Exploring around, here is a solution that seems to work in both R2012a and R2012b. It involves obtaining a handle to the underlying JTextArea of the command window, which is used to get the selected text (to evaluate size of selected variable name)
Create a shortcut with the following code:
x = com.mathworks.mde.cmdwin.XCmdWndView.getInstance();
s = char(x.getSelectedText());
if isvarname(s) && exist(s,'var')
eval( sprintf('size(%s)',s) );
end
Next highlight a variable name in the command window and execute the shortcut. The size will be immediately printed as shown in the screenshot below:
It is not very nice as it is an external solution, but this is how it could work:
Assuming you are in the command window and want to evaluate size(var_a) by selecting it, you can probably do this with a keyboard macro. Defind the appropriate function of var_a
f(x) = eval('size(' x ')'
%This could be done in the macro, but nicer to do it here for easy editing.
Then make sure your macro does this:
Copy 'var_a'
Turn it into 'f(var_a)'
Paste the result
Hit enter
Like i said, it's not pretty, but it should do the trick.