首页 > 解决方案 > 在 Matlab 文本窗口(uicontrol)上自动向下滚动

问题描述

我想使用 Matlab 连续读取文件并将其显示在专用窗口中。所以我使用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}));

标签: matlabuser-interfacematlab-figure

解决方案


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

首先需要的是findjobj来自 Matlab 中心的实用程序。您需要下载此函数并在您的 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) );

等等瞧:-)


推荐阅读