Keyboard shortcut to evaluate current line?

324 ビュー (過去 30 日間)
Chad Greene
Chad Greene 2014 年 6 月 2 日
コメント済み: Rozanne 2024 年 3 月 6 日
I often use a keyboard shortcut to evaluate highlighted selections of a script I'm working on. But often, I will add a single line to a script and want to evaluate it right away. To do this I highlight the line and hit F9, but it would be a smoother process if I could simply hit F10 to evaluate the line my cursor is on without needing to highlight the line. I've checked the Preferences and I don't see this option available. Are there any clever hacks to allow this?
I'm using R2012b on a Mac.

採用された回答

Eric
Eric 2015 年 3 月 27 日
編集済み: Eric 2015 年 3 月 27 日
Create a Quick Access Toolbar shortcut for doing this and call it "Run line". Here's the code:
currentEditor = matlab.desktop.editor.getActive;
originalSelection = currentEditor.Selection;
assert(originalSelection(1)==originalSelection(3));%Check that multiple lines are not selected
currentEditor.Selection = [originalSelection(1) 1 originalSelection(1) Inf];%Select the whole line
eval(currentEditor.SelectedText);%Run the whole line
currentEditor.Selection = originalSelection;%Reset selection to original state
clear currentEditor originalselection
Now, press and hold the Alt key (at least in Windows) to see the mnemonics for the Quick Access Toolbar items. On my system a little "5" shows for the "Run line" shortcut.
Hence I can press Alt+5 to run the current line and leave my selection unchanged. Alternatively I could use the mouse to click the "Run line" button on the Quick Access Toolbar.
-Eric
  27 件のコメント
Tahariet Sharon
Tahariet Sharon 2023 年 6 月 5 日
Where do I save this code?
Rozanne
Rozanne 2024 年 3 月 6 日
Save it in the Favorite commands option under Home

サインインしてコメントする。

その他の回答 (5 件)

rrg
rrg 2016 年 4 月 27 日
Also request in Mac

Sean de Wolski
Sean de Wolski 2014 年 6 月 2 日
I don't know of this option but I do understand the use-case and agree that it would be valuable. Here's how I do it right now:
When writing a script, I always use lots of code sections.
Since you can evaluate a section from keyboard or from the "run section" button there is always the option to evaluate the section. If you intelligently name your variables so that they don't overwrite previous ones, then you will never have trouble running the whole section versus just the line of code. I'll also put computationally expensive lines of code in their own section so that it only needs to be rerun if the higher-level sections have changed.
This mimics the functionality you desire and actually expands it to multiple lines at once.
  1 件のコメント
Chad Greene
Chad Greene 2014 年 6 月 2 日
Indeed, I also use sectioning quite a bit. I was hoping for a single-line equivalent. I effectively want a new cell for each line of code without actually adding the %% between each line. It's only a minor inconvenience to highlight some new xlim line and hit F9 after data have already been plotted, but it would be less clunky if the current line could be evaluated with a single keystroke.

サインインしてコメントする。


Benjamin Bechtel
Benjamin Bechtel 2014 年 8 月 19 日
Unfortunately I don't have a answer, but I'd be interested too. Did you find a way?
  3 件のコメント
Kelly Kearney
Kelly Kearney 2014 年 8 月 19 日
編集済み: Kelly Kearney 2014 年 8 月 19 日
I'll add my vote for this feature too. It's perhaps a minor inconvenience to have to highlight a line, but it's one of those little things I repeat dozens of times each day.
Brian
Brian 2015 年 3 月 27 日
I agree wholeheartedly and would love to see this feature request completed! How do we go about getting our request known?

サインインしてコメントする。


Joseph Cheng
Joseph Cheng 2014 年 8 月 19 日
編集済み: Joseph Cheng 2014 年 8 月 19 日
This is not entirely 1 button press but you can use the documentation here http://blogs.mathworks.com/community/2009/09/28/configurable-keyboard-shortcuts-have-arrived/ and configure a shortcut to do this. My test (in windows R2011A and R2013b) was to assign "evaluate selection" to F8 and "Selection End Line" to F8. I kept the "limit" keystroke selection to 1.
So by doing this F8 now either:
  1. if something is selected runs the selection
  2. with nothing selected the first F8 press highlights from my cursor to the end of line then upon second F8 press it runs selection.
I see now that i could have just mapped it to F9 as its performance is conditional to what is selected.
Not entirely sure how much faster this is. Normal keyboard way would be home->shift+end->F9 which is just 4 presses. My method would save you a key press as its home->F8->F8.
  1 件のコメント
Brian
Brian 2015 年 3 月 27 日
I can't get this to work in R2015A. They list a possible conflict between the two actions and F8 ends up only evaluating, it does not perform selection end line.
BUT, I did discover that single clicking the white margin to the left of your text (between the grey debug "-" (after the line number) and the first letter on your line) will select the entire line. Thus, clicking that margin and then pressing F9 completes what we are trying to accomplish... still wish we could just have one keybind for evaluating the current line.

サインインしてコメントする。


Rodrigo Coelho
Rodrigo Coelho 2018 年 3 月 3 日
Anybody knows how to get the keyboard mnemonics for Mac?
  2 件のコメント
Walter Roberson
Walter Roberson 2018 年 3 月 3 日
I am not certain I understand what you are asking, but have you looked Preferences -> Keyboard -> Shortcuts ?
Arun Das
Arun Das 2018 年 3 月 31 日
On Mac, select the lines to run; then press:
fn + shift + F7

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDesktop についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by