GUI pop-up menu in MATLAB

19,806

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.)

Share:
19,806
Chethan
Author by

Chethan

Updated on June 08, 2022

Comments

  • Chethan
    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
    Chethan about 11 years
    Well the function to be called is separate function and it is not at all related to GUI window function to use handles.
  • Chethan
    Chethan about 11 years
    Well the function to be called is separate function and it is not at all related to GUI window function to use handles. code mentioned above for i = 1:n % Store top n matches... is outside function and it is not in GUI window function.
  • wakjah
    wakjah about 11 years
    OK but you have access to handles in the search button callback, so you can obtain the value of n in that callback and pass it to your separate function as an argument.
  • Chethan
    Chethan about 11 years
    Yeah 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
    Chethan about 11 years
    Sorry 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
    wakjah about 11 years
    Unless 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 argument n, and this then performs the loop, the value of n cannot be changed by the GUI while the function is executing. I really don't see what the problem is...
  • Molly
    Molly about 11 years
    This 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
    Chethan about 11 years
    sorry again, which is the opening function?
  • Molly
    Molly about 11 years
    If 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
    Chethan about 11 years
    new 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
    Chethan about 11 years
    new 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
    wakjah about 11 years
    Please make this a new question to avoid cluttering this one.