在Matlab文本窗口上自动向下滚动(uicontrol)

在Matlab文本窗口上自动向下滚动(uicontrol),matlab,user-interface,matlab-figure,Matlab,User Interface,Matlab Figure,我想使用Matlab连续读取一个文件,并在专用窗口中显示它。因此我使用uicontrol命令。它工作得很好,但是每次我更新内容时,我都想直接在内容的末尾。有什么解决办法吗 MWE: figHandle = figure('units','pixels',... 'position',[40 40 240 940],... 'menubar','none',... 'resize','off',...

我想使用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}));
没有纯粹的MATLAB方法可以做到这一点,但完全可以使用未记录的方法操作底层java组件

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

一旦您访问了文本框的java方法,将插入符号移动到文本末尾就很简单了,您只需要调用组件方法之一:
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) );

et voila:-)

非常感谢。另一种解决方案是使用
jTxtPane.getDocument.getLength
代替
numel(txt)
,但需要在前面添加一个
pause(0.1)