How do you retrieve the selected text in MATLAB?

前端 未结 3 1265
伪装坚强ぢ
伪装坚强ぢ 2021-01-05 23:07

MATLAB has several selection-sensitive capabilities. For example, if you select some text and press F9, it evaluates your selection. (Unless you\'ve remapped your keyboard

3条回答
  •  误落风尘
    2021-01-05 23:50

    In case you want to use something like this but with text highlighted in the editor rather than in the command window.

    I use the following code in order to be able to quickly check the nnz() of a variable, although you can change the code in the nested try-catch to whatever you need.

    Lastly, I created a shortcut with this code in the top right of Matlab, which I access quickly by pressing Alt-1.

    try
        activeEditor = matlab.desktop.editor.getActive;
        currentSelection = activeEditor.SelectedText;
    
        try
            eval(sprintf('val = nnz(%s);',currentSelection))
            disp(sprintf('>> nnz(%s) = %s',currentSelection,num2str(val)))
        catch ex
            disp(ex.message)
        end
    catch ex
        disp(ex.message)
    end
    

提交回复
热议问题