Get string value from GUI listbox
显示 更早的评论
I have created a GUI and selected the listbox to list lots of countries.


I then need to know what country the user has selected. The text box should update accordingly. I have used this code:
ListBoxCheck = get(handles.listbox1,'String');
if(ListBoxCheck == Afghanistan)
set(handles.text3,'String','Working');
elseif(ListBoxCheck == Argentina)
set(handles.text3,'String','Working2');
end
However, when I click on Argentina or Afghanistan I get this: Undefined function or variable 'Afghanistan'.
Error in VisaProcessor>listbox1_Callback (line 135) if(ListBoxCheck == Afghanistan)
Anyone know why or can help?
Thank.s
1 个评论
To get the selected value you need to use the Value property, not the String property.
There are also several major syntax errors in if(ListBoxCheck == Afghanistan) that help generate that error:
- Afghanistan is not a string, so MATLAB expects it to be a variable or function and looks for one with that name, which it then cannot find. To make it a string, use single quotes: 'Afghanistan'.
- ListBoxCheck == 'Afghanistan' using the equivalence operator does an element-by-element comparison of the two strings, checking if each character in each position is the same. It will be an error if the strings are different lengths. To see if strings are the same, use strcmp instead.
采纳的回答
更多回答(4 个)
Yoav Livneh
2015-2-17
Change the condition on your if to
if strcmp(ListBoxCheck, 'Afghanistan')
Hopefully this should now work.
1 个评论
Yoav Livneh
2015-2-17
After reading a little bit more about listboxes I know what the problem is. When you do
ListBoxCheck = get(handles.listbox1,'String');
you get a cell array of strings with all the countries. The if condition will not work this way.
The property that changes when you click on a name is the Value property. So instead of what you have, use
listBoxStrings = get(handles.listbox1,'String');
ListBoxValue = get(handles.listbox1,'Value');
if(find(strcmp(ListBoxString,'Afghanistan')) == ListBoxValue)
set(handles.text3,'String','Working');
elseif(find(strcmp(ListBoxString,'Argentina')) == ListBoxValue)
set(handles.text3,'String','Working2');
end
5 个评论
The strcmp documentation states quite clearly that you can compare multiple strings if you put them in a cell array:
strcmp('Australia',{'United Kingdom','Australia','Thailand'})
However in your case you are trying to compare different numbers of strings, which will not work using this method. Instead, you could try using ismember .
JR
2015-2-24
The same thing again: you are trying to enter each string as a separate arguments. Your code
ismember(listBoxStrings,'Afghanistan','Albania')
has three input arguments to ismember: the first two are supposed to be the data you want to compare, the third is an optional flag. So ismember thinks that 'Albania' is an option flag, but it does not match with any flags that it accepts. Thus the error.
To fix this simply place the strings in a cell array. This array is then entered as the second argument of ismember, exactly as the documentation clearly requires:
ismember(listBoxStrings,{'Afghanistan','Albania'})
Note the curly brackets which define the cell array.
Tip: don't clutter your work with unnecessary parentheses and semi-colons. It is much clearer to write this, for example:
if ismember(listBoxStrings,'Afghanistan')
and
if ismember(listBoxStrings,{'Afghanistan','Albania'})
JR
2015-2-24
Using find does not really help this situation. You probably want to use all or any instead:
if any(ismember({'Afghanistan','Albania'}, listBoxStrings))
JR
2015-2-27
1 个评论
ismember works just fine for locating which strings are members of another cell array of strings, just like we want to do here.
There are two issues that should be fixed to make this work properly. Both of them are clearly described in the documentation, so lets have look:
The documentation for ismember states: ismember(A,B) returns an array containing 1 (true) where the data in A is found in B. Elsewhere, it returns 0 (false). So actually your comparisons have the arguments around the wrong way: the string/s that you want to check for should be the first argument to ismember, whereas listBoxStrings should be the second.
The documentation for find states: find(X) returns a vector containing the linear indices of each nonzero element in array X. Why did you add find? How do you expect indices to work in an if statement? Because MATLAB indices start from one, and all non-zero values are considered to be true, then the only case where if [some indices} is not going to evaluate the following statement is when some_indices is empty. For any indices at all this statement is going to be true, which is not very useful for this situation.
You might like to consider using any instead of find, which probably provides the output condition that you want. Altogether we get something like this:
if any(ismember({'Afghanistan','Albania'}, listBoxStrings))
PS: Although you keep writing them, the semicolons are not required after an if statement.
类别
在 帮助中心 和 File Exchange 中查找有关 Entering Commands 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
