自动向下滚动 Matlab 文本 window (uicontrol)

Automatic scroll down on Matlab text window (uicontrol)

我想使用 Matlab 连续读取文件并在专用 window 中显示它。所以我使用 uicontrol 命令。它运行良好,但我想在每次更新内容时直接转到内容末尾。有解决办法吗?

MWE:

figHandle = figure('units','pixels',...
                'position',[40 40 240 940],...
                'menubar','none',...
                'resize','off',...
                'numbertitle','off',...
                'name','window custom')
txHandle = uicontrol('style','edit',...
                'units','pix',...
                'position',[10 60 220 830],...
                'backgroundcolor','w',...
                'HorizontalAlign','left',...
                'min',0,'max',10,...
                'enable','inactive');
txt=repmat('t|',1,100000);
set(txHandle,'string',cat(1,get(txHandle,'string'),{txt}));

没有纯 MATLAB 这样做的方法,但完全有可能,使用 未记录 方法,操纵底层 java个组件。

首先需要的是来自 Matlab Central 的实用程序 findjobj。您需要下载此函数并在您的 MATLAB 路径中访问它。此函数将检索 MATLAB 文本框下的 java 对象的句柄。

一旦您可以访问文本框的 java 方法,将 caret 移动到文本末尾就很简单了,您只需要调用组件方法之一:setCaretPosition(positionIndex).

在您的 MATLAB 路径中包含函数 findjobj 后,只需在您的示例代码之后添加此代码:

% Get the handle of the jave edit box
jtxtBox = findjobj(txHandle) ;
% Get the handle of the jave "panel" component
jTxtPane = jtxtBox.getComponent(0).getComponent(0) ;
% move the caret to the end of the text
jTxtPane.setCaretPosition( numel(txt) );

瞧瞧:-)