Returna value to the main function from 'Callback'

4 views (last 30 days)
Max
Max on 8 Jun 2016
Answered: Jan on 2 Mar 2017
Hello everyone,
I have a few issues with the appropriate use of the 'Callback' in the uicontrol. Generally I want to open a prompt figure with three different subplots in my script, each showing a different fit to experimental data. Beneath those plots I have arranged three buttons to allow the user to choose a specific fitting scheme. Clicking each of the buttons should assign a different value to the variable 'button_choice' (namely: 1, 2 or 3). A simplified version of my main script looks like this:
fig = figure(1);
% maximize figure
set (fig, 'Units', 'normalized', 'Position', [0,0,1,1]);
% for the sake of simplicity the actual plots have been removed
d = gcf;
% define button 1
btn1 = uicontrol('Parent',d,...
'Position',[200 100 330 100],...
'String','Choose Fitting Scheme: Spline',...
'Callback','[button_choice] = press_button_1');
% define button 2
btn2 = uicontrol('Parent',d,...
'Position',[600 100 330 100],...
'String','Choose Fitting Scheme: Linear',...
'Callback','[button_choice] = press_button_2');
% define button 3
btn3 = uicontrol('Parent',d,...
'Position',[1000 100 330 100],...
'String','Choose Fitting Scheme: n-th order Polynomial',...
'Callback','[button_choice] = press_button_3');
% trigger uiwait to delay further execution until a value has been designated;
% the figure is deleted in the function age_radius_correlator_button
uiwait
The corresponding functions for the callback look like this, of course with appropriate changes for button 2 and 3:
function press_button_1(object_handle,event)
global button_choice
button_choice = 1;
delete(gcf)
end
So here is my issue:
Right now I use three different functions to assign the three individual values to the variable button_choice, which strikes me as fairly inefficient. Is there a way that I can create a single function 'press_button' that returns an individual value depending on an input value defined within the 'Callback' syntax?
  1 Comment
Joseph Cheng
Joseph Cheng on 8 Jun 2016
Why not use a radio button group to select the type of fitting scheme and then use a button to determine which one was selected and perform fit?

Sign in to comment.

Answers (1)

Jan
Jan on 2 Mar 2017
Defining a callback as string is deprecated because it is prone to bugs and misconceptions. It supported for backward compatibility only. Prefer function handles:
btn1 = uicontrol('Parent',d,...
'Position',[200 100 330 100],...
'String','Choose Fitting Scheme: Spline',...
'Callback', @press_button_1);
Using global variables or variables in the base workspace is another typical source for problems. Better use variables stored locally in the GUI:
function myGUI
handles.fig = figure(1);
... Create polts and buttons
guidata(handles.fig, handles); % Store handles struct in the figure;
end
function press_button_1(object_handle,event)
handles = guidata(object_handle); % Get handles struct from GUI
handles.button_choice = 1;
% delete(gcf): No, don't delete it now.
end
Deleting the GUI using the button might be required in your current workflow. But it would be more reliable to trigger further computations from an existing GUI.
But if you have good reasons to use the GUI to change variables in the base workspace, use:
evalin('base', 'button_choice', 1);
See http://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval for arguments, why this is a bad idea. But if your problem is solved by a short hack already, this works. Keep in mind, that short working hacks tenmd to be reused and expanded in the future and when the code grows, this initially introduced bad concept might impede the work.

Categories

Find more on Graphics Object Properties in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!