Shika93
Shika93

Reputation: 659

Scroll down listbox automatically

I'm using Matlab R2018a where there is no scroll function. I just need to scroll down the listbox as items are added (dynamically).

I found this solution on Google but is not working for me.

handles.slider.Max = length(handles.listbox.String);
handles.slider.Value = handles.slider.Max;

Is there any other solution?

Upvotes: 0

Views: 269

Answers (1)

zeeMonkeez
zeeMonkeez

Reputation: 5157

In order for the ListboxTop property to have an effect, the UI has to be redrawn (using drawnow) after adding the items to the listbox.

Example, using GUIDE with one listbox tagged as listbox1:

In the GUI's OpeningFcn, write

handles.t = timer('BusyMode', 'drop', 'ExecutionMode',...
'fixedRate', 'StartDelay', 4, 'Period', 4.0, 'TimerFcn', {@addItem, hObject});
handles.ctr = 0;
handles.t.start();
guidata(hObject, handles);

At the end, add

function addItem(hObject, EventData, parentO)

handles = guidata(parentO);
handles.ctr = handles.ctr + 1;
handles.listbox1.String{end+1} = sprintf('Item %i', handles.ctr);
drawnow
handles.listbox1.ListboxTop = numel(handles.listbox1.String) ;
guidata(parentO, handles);

Note that when uncommenting the drawnow line, the listbox will always jump to the selected item (the first one by default).

Upvotes: 1

Related Questions