Reputation: 431
I'm working on a project where I use a Matlab GUI to switch between audio outputs real-time on button press. I have a Matlab GUIDE file and a function using the Audio System Toolbox that listens to GUI-actions.
The GUIDE file has an OpeningFcn where I declare various handles before the GUI becomes visible. In the OpeningFcn I declare “handles.AttenuationFactor”. It is the idea that when the user later presses the buttons in the GUI the handles.AttenuationFactor changes accordingly.
I have no problem making the separate function listen to GUI-actions. I simply retrieve the GUI handles from the right GUI object:
gui = findobj('Tag','GUI’);
if ~isempty(gui)
% get handles
gdata = guidata(gui);
AttenuationFactor = gdata.AttenuationFactor;
end
%% Perform calculations with attenuation factor
Here’s the pickle: When do I call the function that listens to the GUI?
I cannot call it in the OpeningFcn because the last line of code guidata(hObject, handles) hasn’t been called yet and when retrieving the information from ‘GUI’ ‘handles.attenuationFactor’ therefore does not exist!
I have tried putting the function call in the callback function of the buttonpress, but that would mean creating code that should only be executed once to be evaluated upon every buttonpress.
Is there some way to force the handles to be updated before the function call?
Upvotes: 0
Views: 360
Reputation: 502
In the opening, you need to set a base value. Call a global variable maybe and set the value. Update the handles accordingly.
You should use the set/get functions from matlab to deal with the handles. What do you mean by setting the handles.attenuationFactor?
If the user changes the value of the attenuationFactor, once it changes, on the callback of the button, you can retrieve the value pretty straightforward.
get(handles.attenuationFactor,'Value')
for instance.
Upvotes: 1