Creating a Matlab run line shortcut (like R)

  1. On the Home tab, click New, and then select Command Shortcut.

  2. In the Label field: enter a name for the shortcut. In the Callback field:

    currentEditor = matlab.desktop.editor.getActive; originalSelection = currentEditor.Selection; assert(originalSelection(1)==originalSelection(3)); currentEditor.Selection = [originalSelection(1) 1 originalSelection(1) Inf]; disp(currentEditor.SelectedText); eval(currentEditor.SelectedText); currentEditor.Selection = originalSelection + [1 0 1 0];

Now I can run the line I'm on by pressing Alt+s+1 (perhaps you can change this to an arbitrary hotkey). I hope this helps.

EDIT: In MATLAB R2018a Command Shortcuts have been repackaged as Favorite Commands. So to create a new shortcut in this and later version, you need to go Home tab -> Favorites -> New Favorite.

EDIT: You currently (in MATLAB R2020b) can run this code with Alt+1 (no need for 's' as above). However, it does not appear to be changeable in Preferences -> Keyboard -> Shortcuts

Tags:

Macos

Matlab