- 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
조회 수: 10 (최근 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!