R has a great shortcut that runs the line your cursor is currently on then moves the cursor onto the next line (cmd + return
). In matlab, you have to highlight the line then run the highlighted section (shift + F7
).
Is there a way to create an 'R like' run line shortcut? I'm using OSX.
And it's a pretty simple MATLAB shortcut. For MAC as the command key (⌘) and return or F5 on other platforms. If you don't want to run the whole code, select a fragment and press F9 instead for PC or function and F9.
To customize a keyboard shortcut: Open the Keyboard Shortcuts Preferences page in the Preferences Window and, in the search box, type an existing keyboard shortcut or the name of an action, tool, or menu. For example, you can type Ctrl+R (shortcut), Delete (action), Command Window (tool), or File (menu).
You can create a shortcut to the desktop or you can add MATLAB to your Start Menu. To create a shortcut, right-click on matlab.exe and select “Send to >” and then select “Desktop (create shortcut)”. To add MATLAB to the Start Menu, right-click on matlab.exe and select “Pin to start.”
On the Home tab, click New, and then select Command Shortcut.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With