Reputation: 213
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.
Upvotes: 3
Views: 20118
Reputation: 13610
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.)
Upvotes: 1
Reputation: 4551
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.
Upvotes: 2