Get string value from GUI listbox

15 次查看(过去 30 天)
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 个评论
Stephen23
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:
  • 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.

请先登录,再进行评论。

采纳的回答

JR
JR 2015-2-17
编辑:Walter Roberson 2018-10-4
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
Answered by Yoav Livneh

更多回答(4 个)

Yoav Livneh
Yoav Livneh 2015-2-17
Change the condition on your if to
if strcmp(ListBoxCheck, 'Afghanistan')
Hopefully this should now work.

JR
JR 2015-2-17
编辑:JR 2015-2-17
No errors, but with the code you have supplied it doesn't update the text box. I have edited the tag since last time and updated it in the code.
ListBoxCheck = get(handles.listbox1,'String');
if strcmp(ListBoxCheck, 'Afghanistan')
set(handles.textbox,'String','Working');
elseif strcmp(ListBoxCheck, 'Albania')
set(handles.textbox,'String','Working2');
end
  1 个评论
Yoav Livneh
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
JR 2015-2-23
编辑:JR 2015-2-23
I have almost come to the end of my code! I now need to save the image. I have two global variables (AdjustedImage and BlankPaper). A lot of countries share the same sizes for my project. Say, UK, Thailand, Australia for example are 45mmx35mm. I wanted to make my code shorter, so thought about using the same style of code above:
global AdjustedImage;
global BlankPaper;
imtodouble = im2double(AdjustedImage);
BPaper = im2double(BlankPaper);
pause(3);
listBoxStrings = get(handles.listbox1,'String');
ListBoxValue = get(handles.listbox1,'Value');
if(find(strcmp(listBoxStrings,'United Kingdom','Australia','Thailand')) == ListBoxValue)
set(handles.text3,'String','Saving this image to 2 photos on a 6x4"!');
ImageRowCount = 1;
ImageColumnCount = 2;
end
Other countries, will have different ImageRowCount/ImageColumnCount
The error is 'too many input arguments'. I know that strcmp compares two strings, if strcmp is not a good option. What would be viable?
Thanks!
  5 个评论
JR
JR 2015-2-24
Thankyou for the detailed explanation, I now understand where I was going wrong! I have adjusted the code slightly, as it would not work with some variables I have defined, after this piece of code.
Using:
if find(ismember(listBoxStrings,{'Afghanistan','Albania'}));
Stephen23
Stephen23 2015-2-27
编辑:Stephen23 2015-2-27
Using find does not really help this situation. You probably want to use all or any instead:
if any(ismember({'Afghanistan','Albania'}, listBoxStrings))

请先登录,再进行评论。


JR
JR 2015-2-27
The ismember code, perhaps isn't doing the correct trick.
listBoxStrings = get(handles.listbox1,'String'); %
ListBoxValue = get(handles.listbox1,'Value'); %
if find(ismember(listBoxStrings,{'Afghanistan'}));
set(handles.text3,'String','Saving this image to 6 photos on a 6x4"!'); % SETS THE TEXTBOX TO THE DEFINED VALUE
ImageRowCount = 2;
ImageColumnCount = 3;
ADDITIONAL CODE.......;
then
elseif find(ismember(listBoxStrings,{'Nepal'}));
set(handles.text3,'String','Saving this image to 4 photos on a 6x4"!'); % SETS THE TEXTBOX TO THE DEFINED VALUE
ImageRowCount = 2;
ImageColumnCount = 2;
When I choose Afghanistan when I process the image, the code still works and I get 6 photos on a sheet. :)
When I choose Nepal instead, I get 6 photos per sheet :(, not 4!
Interestingly, I put an end at the end of the first if, before else if and changed elseif to if and put another end at the end. It does the code for the first if statement, then does the second. Which to me indicates that the ismember code isn't being registered?
Cheers.
  1 个评论
Stephen23
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 CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by