代码之家  ›  专栏  ›  技术社区  ›  Richie Cotton Joris Meys

如何在MATLAB中检索选定的文本?

  •  5
  • Richie Cotton Joris Meys  · 技术社区  · 15 年前

    MATLAB有几个选择敏感的功能。例如,如果您选择一些文本并按F9键,它将评估您的选择。(除非已重新映射键盘设置。)

    disp(GetSelection()) .

    但这里面有什么 GetSelection ?

    3 回复  |  直到 15 年前
        1
  •  5
  •   Community CDub    8 年前

    感谢 @Yair Altman undocumented Matlab

    将其放入快捷方式(或快捷方式调用的函数):

    %# 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)
    
        2
  •  0
  •   Patrick Mineault    15 年前

    我不相信有任何方法可以控制或读取Matlab文本编辑器中的选择,在Mathworks网站上没有提到这样的API(至少从Google上的快速搜索)。如果希望此功能启用更高级的文本编辑,则可以考虑将.m文件编辑器设置为外部编辑器( http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_env/brxijcd.html

        3
  •  0
  •   positronidude    10 年前

    如果您想使用这样的东西,但是文本在编辑器中而不是在命令窗口中高亮显示。

    我使用下面的代码是为了能够快速检查变量的nnz(),尽管您可以根据需要更改嵌套try catch中的代码。

    最后,我用Matlab右上角的代码创建了一个快捷方式,我可以通过按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