- 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
15 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
JR
el 17 de Feb. de 2015
Editada: Walter Roberson
el 4 de Oct. de 2018
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 comentario
Stephen23
el 17 de Feb. de 2015
Editada: Stephen23
el 17 de Feb. de 2015
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:
Respuesta aceptada
Más respuestas (4)
Yoav Livneh
el 17 de Feb. de 2015
Change the condition on your if to
if strcmp(ListBoxCheck, 'Afghanistan')
Hopefully this should now work.
0 comentarios
JR
el 17 de Feb. de 2015
Editada: JR
el 17 de Feb. de 2015
1 comentario
Yoav Livneh
el 17 de Feb. de 2015
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
el 27 de Feb. de 2015
1 comentario
Stephen23
el 27 de Feb. de 2015
Editada: Stephen23
el 27 de Feb. de 2015
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.
Ver también
Categorías
Más información sobre Characters and Strings en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!