Hello, I was looking for a way to control further the possibility of selection (modification of the property 'Value' of a listbox) to make only the items who are present in the listbox. Indeed as seen in the image,
there is only four items in the listbox, but it is possible to select a non existent item by being able to select 'Value' that will be off limit from the box. It happen to cause an error when i use a delete function because of this over permissive selection. After many research on the forum and other place I couldn't manage to find ways to limit the value of the 'Value' property.
Thanks in advance for your help and time.
ps : I'm using the Matlab 2011b version

댓글 수: 4

Adam
Adam 2017년 12월 19일
This is not standard behaviour. If you just create a listbox and set its string you can normally only select the items that actually exist.
I am using R2017b, but I don't remember it ever being otherwise for normal usage. How do you get to the situation shown? Was it a user selection or did it just happen to be what was selected after you deleted some items?
Image Analyst
Image Analyst 2017년 12월 19일
The items must exist but be strings of spaces. See my Answer below
@Adam Indeed you are right it appear that the property Value is acting strangely and as Image Analyst speculated, it look like there is empty elements created during the populating process. Here is the code I use to populate the String property.
handles.counter is a variable that count the action stocked in a list of struct. I think the problem may come from the | who appear to be some special character in Matlab if I'm right.
if handles.counter == 0 %if no action print an empty list and prevent negative counter
lboxActions = ('');
set(handles.pushbutton_removeAction,'Enable','off');
else
lboxActions = cell(handles.counter); %pre-allocation
for i=1:handles.counter %check for the type of movement for adequat display of the speed
if handles.actionList(handles.counter).movement == 1
infoAction = strcat(num2str(i),'|spd:',num2str(handles.actionList(i).speed),'|',num2str(handles.actionList(i).duration),'|',num2str(handles.actionList(i).delayBefore),',',num2str(handles.actionList(i).delayAfter));
else
infoAction = strcat(num2str(i),'|spd:-',num2str(handles.actionList(i).speed),'|',num2str(handles.actionList(i).duration),'|',num2str(handles.actionList(i).delayBefore),',',num2str(handles.actionList(i).delayAfter));
end
lboxActions{i} = infoAction; %compile cell array of names.
end
set(handles.listbox_actionList,'Max',handles.counter+1) % putting the max up to allow display of the adequat number of action
set(handles.listbox_actionList,'String',lboxActions);%print
Btw it look like the function of listBox have changed since 2011b because I can't find any property call ItemsData
Image Analyst
Image Analyst 2017년 12월 20일
There were a lot of changes starting with R2014b. What version are you using?
Did my solution below work?
What is handles.counter? Is it supposed to be the number of items in the listbox? Why are you setting the 'Max' property? As I remember it, it was just 1 or 2 depending on whether you want it to be single selection or multiselection, not some arbitrary number.

댓글을 달려면 로그인하십시오.

답변 (1개)

Image Analyst
Image Analyst 2017년 12월 19일

0 개 추천

Try this:
listBoxItems = handles.listbox1.String;
selectedItems = [];
for k = 1 : length(listBoxItems)
if length(strtrim(listBoxItems{k})) > 0
% Only select items that are not all whitespace.
selectedItems = [selectedItems, k];
end
end
handles.listbox1.Value = selectedItems;

댓글 수: 2

ET1994
ET1994 2018년 11월 17일
Image Analyst, I want to choose a care from the listbox and set an if statement if one is chosen from the list the corresponding x% appear in a static Text.
Can you help for the coding?
Capture.JPG
selectedItem = handles.listbox1.Value
if selectedItem == 3
% Accident, so do whatever. It's the third item after Keyword and a blank line.
elseif selectedItem == 4
% Childbrith, so do whatever.
etc.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Entering Commands에 대해 자세히 알아보기

제품

질문:

2017년 12월 19일

댓글:

2018년 11월 17일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by