- 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.
Get string value from GUI listbox
9 次查看(过去 30 天)
显示 更早的评论
I have created a GUI and selected the listbox to list lots of countries.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/147170/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/147171/image.png)
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 个评论
Stephen23
2015-2-17
编辑:Stephen23
2015-2-17
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:
采纳的回答
更多回答(4 个)
Yoav Livneh
2015-2-17
Change the condition on your if to
if strcmp(ListBoxCheck, 'Afghanistan')
Hopefully this should now work.
0 个评论
JR
2015-2-17
编辑:JR
2015-2-17
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
JR
2015-2-27
1 个评论
Stephen23
2015-2-27
编辑:Stephen23
2015-2-27
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!