Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to deselect item in a listbox?

401 views
Skip to first unread message

New

unread,
Feb 4, 2006, 9:46:38 PM2/4/06
to
In a multi-select listbox, how can we deselect items, Has anyone come
across a way to do
this? (deselect so that no items are selected)

Thanks

nico

unread,
Feb 5, 2006, 9:50:27 AM2/5/06
to

Hello,
I think you must be change the Value property.
For a multiselect, you have a double array for this, replace this
array by an another one (for instance, the same which less elements)

Yoav Mor

unread,
Feb 5, 2006, 10:05:52 AM2/5/06
to
You indeed do that with the "value" property. example:

ui = uicontrol
('style','listbox','String',{'one';'two';'three'},'Units','Pixels','Po
sition',[20 20 400 200],'Max',3);

set (ui,'value',[1 2 3]); % will select all of them
set (ui,'value',[1 2]); % will select only the first two
set (ui,'value',[]); % will deselect everything

Lockywolf

unread,
Apr 15, 2016, 3:00:10 AM4/15/16
to
"Yoav Mor" <yo...@a.co.il> wrote in message <ef27...@webx.raydaftYaTP>...
Doesn't work any more.

Returns:

Warning: Single-selection 'listbox' control requires that 'Value' be an integer within String range
Control will not be rendered until all of its parameter values are valid

Steven Lord

unread,
Apr 15, 2016, 9:18:17 AM4/15/16
to


"Lockywolf" <lock...@gmail.com> wrote in message
news:neq3hj$4bu$1...@newscl01ah.mathworks.com...
Works fine for me in release R2016a. I'm guessing from the warning that you
didn't set the 'Max' property of the listbox. See the descriptions of the
Max and Min properties in the "Type of Control" section of the documentation
page discussing uicontrol properties.

http://www.mathworks.com/help/matlab/ref/uicontrol-properties.html

--
Steve Lord
sl...@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Image Analyst

unread,
Apr 24, 2016, 5:03:08 PM4/24/16
to
New:

You have to get the selection first, then remove the indexes you don't want. To see what is selected, you can do this:

selectedItems = handles.listFileList.Selection % Get which are highlighted.

Note the numbers. For selected items, the number will in the list, like [1, 4, 5, etc.]. If the item is not selected, it's not in the list. So you need to update the selection. So let's say you didn't want #5 anymore - you want to deselect it. So you come up with a new selection. A nice trick, the setdiff() function can remove numbers you don't want from an array:

% Starting with [1,4,5], select all but 5. (i.e. remove the 5)
handles.listFileList.Selection = setdiff(handles.listFileList.Selection, 5);

If your selection has negative numbers, then you have a really old version of MATLAB and need to set that index to a negative number. Like if #3 is not selected, the value of the selection array at index #3 will be -3 or -4 or something like that. Let me know if that's the case and I'll try to help.

Lockywolf

unread,
Jul 5, 2016, 3:38:10 PM7/5/16
to
>>See the descriptions of the Max and Min properties in the "Type of Control" section of the documentation

My 'Max' property is 1 and 'Min' is 0.

I am not trying to get multiple indices selected, it am trying to remove the selection all together.

]>set( handles.lst_projects, 'value', [] )
Warning: Single-selection 'listbox' control requires a scalar Value.
Control will not be rendered until all of its parameter values are valid

And the control disappears.


"Steven Lord" <Steve...@mathworks.com> wrote in message <neqpmh$l13$1...@newscl01ah.mathworks.com>...

Steven Lord

unread,
Jul 7, 2016, 10:36:30 AM7/7/16
to


"Lockywolf" <lock...@gmail.com> wrote in message
news:nlh2as$845$1...@newscl01ah.mathworks.com...
>>>See the descriptions of the Max and Min properties in the "Type of
>>>Control" section of the documentation
>
> My 'Max' property is 1 and 'Min' is 0.
>
> I am not trying to get multiple indices selected, it am trying to remove
> the selection all together.
>
> ]>set( handles.lst_projects, 'value', [] )
> Warning: Single-selection 'listbox' control requires a scalar Value.
> Control will not be rendered until all of its parameter values are valid
> And the control disappears.

Since Max-Min is 1, your listbox is a single selection listbox and that
requires that exactly one item be selected -- no more, no fewer. Change Max
to 2 and leave Min at 0 and you should be able to select a different number
of elements than just 1 -- and that includes 0 elements.

See the description of the Max and Min properties in the "Type of Control"
section on this documentation page for more information.

http://www.mathworks.com/help/matlab/ref/uicontrol-properties.html

*snip*

Lockywolf

unread,
Jul 7, 2016, 3:34:10 PM7/7/16
to
"Steven Lord" <Steve...@mathworks.com> wrote in message <nllpd6$4ve$1...@newscl01ah.mathworks.com>...
Well, indeed, I can deselect a line if I set mylist.Max = 2;

However the following code doesn't work:

mylist.Max = 2;
mylist.Value = [];
mylist.Max = 1;

Fails with the same error: "Control will not be rendered until all of its parameter values are valid"

Basically, when the listbox is created, it is a single-selection listbox and nothing is selected. I just want to return it to the same state.

Yair Altman

unread,
Jul 7, 2016, 6:17:10 PM7/7/16
to
"Lockywolf" wrote in message <nlmarb$5a$1...@newscl01ah.mathworks.com>...
Don't update Max back to 1. Instead, keep Max at 2 and set or modify the callback function (accessible via the control's Callback property) to check the Value property and update it to only contain up to a single value. Something along the following lines (untested):

% Note: this is untested pseudo-code!
function myCallbackFunc(hListbox, eventData)
lastValue = getappdata(hListbox, 'lastValue');
value = get(hListbox, 'Value');
if ~isequal(value, lastValue)
value2 = setdiff(value, lastValue);
if isempty(value2)
setappdata(hListbox, 'lastValue', value);
else
value = value2(1);
setappdata(hListbox, 'lastValue', value);
set(hListbox, 'Value', value);
end
end
end

A better solution that preserves the last-selected item needs to rely on the underlying Java component.

I plan to dedicate next week's blog article to this topic, since I believe that the fact that Max-Min>1 can be used for listbox deselection is indeed undocumented (or at the very least under-documented), and so is the use of the underlying Java component. I'll post the link here once the article is live.

Yair Altman
http://UndocumentedMatlab.com

Yair Altman

unread,
Jul 13, 2016, 11:40:10 AM7/13/16
to
"Yair Altman" <altma...@gmailDEL.comDEL> wrote ...
[snip]
> I plan to dedicate next week's blog article to this topic, since I believe that the fact that Max-Min>1 can be used for listbox deselection is indeed undocumented (or at the very least under-documented), and so is the use of the underlying Java component. I'll post the link here once the article is live.

as promised: http://undocumentedmatlab.com/blog/listbox-selection-hacks

Yair Altman
Undocumented Matlab
http://UndocumentedMatlab.com

0 new messages