I want to use Matlab to read continuously a file and to show it in a dedicated window. So I use the uicontrol
command. It works well but I want to go directly at the end of the end of the content each time I update the content. Is there any solution for doing that?
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}));
There is no pure MATLAB way of doing so, but it is perfectly possible, using undocummented methods, manipulating the underlaying java components.
The first thing needed is the utility findjobj
from Matlab central. You need to download this function and have it accessible in your MATLAB path. This function will retrieve the handle of the java object underlying the MATLAB text box.
Once you have access to the java methods of the textbox, moving the caret
to the end of the text is trivial, you just need to call one of the component method: setCaretPosition(positionIndex)
.
Once you have the function findjobj
in your MATLAB path, just add this code after your example code:
% 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 :-)