Reputation: 883
This is a basic question, but I am having a hard time with it.
Basically, I have a callback-function assigned to the choices in a pop-up menu on a GUI. The code is as follows:
uicontrol(mainfigure, 'Style', 'popup',...
'String', 'A|B|C',...
'Position',[850 190 200 30],...
'Callback', @blockset);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [block] = blockset(hObj,evnt) %#ok<INUSD>
blockval = get(hObj,'Value');
if blockval == 1
block = 'A';
elseif blockval == 2
block = 'B';
elseif blockval == 3
block = 'C';
end
end
As you can see, it is simply assigning a string value to the different choices in the pop-up menu. I want to use these strings as input values to another function later in the script (which is also embedded in a uicontrol callback):
uicontrol(mainscreen, 'Style', 'pushbutton',...
'Position',[855 300 150 50],...
'String', 'START',...
'FontSize',10,'FontWeight','bold',...
'BackgroundColor', [.9 .9 .9],...
'CallBack', {@START_Callback, block});
The code as is doesn't work. But I can't figure out how to define the outputs for a uicontrol callback. I already defined "block" as the output for the blockset function, so how do I get the START_Callback to recognize it as input? Every time I try, it just tells me that "block" is an undefined function or variable.
Is there something I need to do with the " 'Callback', @blockset " line of code to get it to recognize the output from the function?
EDIT: Some cursory internet searching indicates that I probably have to use something like setappdata/getappdata, or another workaround method. However, I don't entirely understand the documentation on those. How do I use them in this situation?
Upvotes: 0
Views: 7587
Reputation: 4194
the variable block would have to exist in the workspace when you do
uicontrol(mainscreen, 'Style', 'pushbutton',...
'Position',[855 300 150 50],...
'String', 'START',...
'FontSize',10,'FontWeight','bold',...
'BackgroundColor', [.9 .9 .9],...
'CallBack', {@START_Callback, block});
But it's the return value from your popup menu callback, so you can't do that, hence your matlab error.
To use setappdata and getappdata, you would need to store your popup menu's callback function's 'block' variable some figure's appdata property that would be visible to both callback functions, or if you want to be lazy, to the root figure. e.g.
function [block] = blockset(hObj,evnt) %#ok<INUSD>
blockval = get(hObj,'Value');
if blockval == 1
block = 'A';
elseif blockval == 2
block = 'B';
elseif blockval == 3
block = 'C';
end
setappdata(0, 'block', block);
end
This will have stored the block variable to the root figure (that is, the main MATLAB window, denoted by 0), which really isn't a good thing to do as anything can change it as well. Instead you should try storing it to some handle graphics object that will be visible to both callbacks, such as your GUI figure. However, there's not enough information in your question for me to infer what you can use, so I'm using the root figure for illustrative purposes.
If you set the tag properties of your GUI objects, you can lookup their handles based on that, e.g. using h = findobj('Tag','my_tag')
will give you the handle to the graphics object with the tag 'my_tag', which you can then set the appdata for via setappdata(h, 'var_name', var);
. I would recommend using this instead of the root figure handle, as with the root figure you have no encapsulation.
With that said, then in your START_Callback
function, instead of taking block
as an input parameter, you'd use block = getappdata(0, 'block');
to get the root figure's block variable which you set in your blockset
callback function. So your pushbutton declaration would become
uicontrol(mainscreen, 'Style', 'pushbutton',...
'Position',[855 300 150 50],...
'String', 'START',...
'FontSize',10,'FontWeight','bold',...
'BackgroundColor', [.9 .9 .9],...
'CallBack', @START_Callback);
and inside START_Callback:
function START_Callback(hObj,evnt)
block = getappdata(0, 'block');
%... other stuff
end
Upvotes: 2