How do you retrieve the selected text in MATLAB?

前端 未结 3 1266
伪装坚强ぢ
伪装坚强ぢ 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:42

    Thanks to @Yair Altman's undocumented Matlab, I was able to figure out the java commands to make this work.

    Put this in a shortcut (or a function that is called by the shortcut):

    %# find the text area in the command window
    jDesktop = com.mathworks.mde.desk.MLDesktop.getInstance;
    try
      cmdWin = jDesktop.getClient('Command Window');
      jTextArea = cmdWin.getComponent(0).getViewport.getComponent(0);
    catch
      commandwindow;
      jTextArea = jDesktop.getMainFrame.getFocusOwner;
    end
    
    %# read the current selection
    jTxt = jTextArea.getSelectedText;
    
    %# turn into Matlab text
    currentSelection = jTxt.toCharArray'; %'
    
    %# display
    disp(currentSelection)
    

提交回复
热议问题