GUI pop-up menu in MATLAB
Solution 1
Well, to start with your switch
statement is incorrect and unnecessary. The Value
property of a dropdown is not the text contained in the current selection, it is the index of the current selection in its list. To get the string value of the list item currently selected, you would do:
contents = cellstr(get(hObject,'String')) % returns contents as cell array
contents{get(hObject,'Value')} % returns value of selected item from dropdown
That is, of course, assuming hObject
is a handle that points to your dropdown box - which it will be only if you're in a callback that was raised by the dropdown itself. Further to that, note that there is no need to convert the string value via a discretised switch
statement; you can just use the str2num
or str2double
functions.
Finally, if you need to access the value of the dropdown from outside one of its own callbacks, you need to use the handles
structure that is passed into every callback (or that, in your sample, is returned from guidata
). There will be a field in handles with the same name as your dropdown - this will be the handle via which you can access its properties.
Solution 2
The way to pass information around a GUI is to use the the handles
structure. If you created your GUI using GUIDE handles
should have been created in the opening function. You can modify the opening function to add field and initial values to handles
. For example, you could add the following to the opening function:
handles.n = 1; % This initializes handles.n to a default value in case the search button is
% pushed before an item in the menu is selected.
Then include the following in the call back for the menu to update and store the value of n:
handles.n = val; % This is updated every time an item from the menu is selected.
guidata(hObject,handles);
In the call back from the search button you can access the value of n and pass it to your other function like this:
n = handles.n;
myFunction(n);
Your other function will have start with something like this:
function [] = myFunction(n)
followed by the rest of the code you included above. You'll have to make sure myFunction.m is in the Matlab search path (can be set using addpath
or by clicking the set path button in Matlab.)
Chethan
Updated on June 08, 2022Comments
-
Chethan almost 2 years
I've a pop-up menu with 5,10,15,20 the contents in that menu. using switch I've created this
val=get(hobject,'value'); switch val case '5' n=5; case '10' n=10; case '15' n=15; case '20' n=20; end guidata(hObject, handles);
where it represents number of output images. On pressing search button in the same GUI window it calls another function where i need to use this 'n'.
for i = 1:n % Store top n matches... tempstr = char(resultNames(index(i))); fprintf(fid, '%s\r', tempstr); disp(resultNames(index(i))); disp(sortedValues(i)); disp(' ') end
How can i pass this 'n' to that code or function? any proper answer is appreciable.
-
Chethan about 11 yearsWell the function to be called is separate function and it is not at all related to GUI window function to use
handles
. -
Chethan about 11 yearsWell the function to be called is separate function and it is not at all related to GUI window function to use
handles
. code mentioned abovefor i = 1:n % Store top n matches...
is outside function and it is not in GUI window function. -
wakjah about 11 yearsOK but you have access to
handles
in the search button callback, so you can obtain the value ofn
in that callback and pass it to your separate function as an argument. -
Chethan about 11 yearsYeah i thought of passing it as an argument, but same function is called multiple times during execution does it make any effect? What i need exactly is if user selects say '10' in the menu, in my for loop 'n' should be assigned to '10'. so that output will be 10 images.
-
Chethan about 11 yearsSorry i didn't get what
handles.n = 1;
will do.. What i need exactly is if user selects say '10' in the menu, in my for loop 'n' should be assigned to '10', Yeah i thought of passing it as an argument, but same function is called multiple times during execution does it make any effect? so that output will be 10 images. -
wakjah about 11 yearsUnless the value of
n
changes while the loop is happening, you should not run into problems. Even then, if your search button callback calls a single function with the argumentn
, and this then performs the loop, the value ofn
cannot be changed by the GUI while the function is executing. I really don't see what the problem is... -
Molly about 11 yearsThis will do what you want. handles.n = 1 initializes the field. The menu call back updates to to the user selected value each time. Then you have to pass an argument to your function. If the menu callback has updated handles.n, your function will have a different input.
-
Chethan about 11 yearssorry again, which is the opening function?
-
Molly about 11 yearsIf you created the GUI with GUIDE, there will be a function called name_OpeningFunction where name is the name you gave to your GUI.
-
Chethan about 11 yearsnew problem, the function to be called has so many calling functions in it. it repeatedly calls the functions so i got an error after passing
n
as argument.??? Undefined variable "handles" or class "handles.queryx". Error in ==> color at 14 D = quadratic(handles.queryx, handles.querymap, X, HSVmap); Error in ==> Search_window>Search_Callback at 109 color(n);
-
Chethan about 11 yearsnew problem, the function to be called has so many calling functions in it. it repeatedly calls the functions so i got an error after passing
n
as argument.??? Undefined variable "handles" or class "handles.queryx". Error in ==> color at 14 D = quadratic(handles.queryx, handles.querymap, X, HSVmap); Error in ==> Search_window>Search_Callback at 109 color(n);
-
wakjah about 11 yearsPlease make this a new question to avoid cluttering this one.