Reputation: 145
I have a button group with 3 radio buttons inside, as well as a display section.
The effect I want is that once choosing a radio button, the display section changes as well.
I implement it using a class, and add the SelectionChangedFcn
when creating those components:
app.ControlButtonGroup.SelectionChangedFcn = {@controlBtnGroupSelectionChanged, app}
I define the function controlBtnGroupSelectionChanged(obj, eventData, app)
at the same file where the createComponents.m function file is saved.
But when I click a different radio, error messages are as following:
Undefined function 'controlBtnGroupSelectionChanged' for input arguments of type 'matlab.ui.container.ButtonGroup'. Error while evaluating ButtonGroup SelectionChangedFcn
I also declare the controlBtnGroupSelectionChanged
function as a private method in my class.
The controlBtnGroupSelectionChanged function is as following:
function controlBtnGroupSelectionChanged(obj, eventData, app)
% Update display section as the radio button in control section is changed
%new = app.ControlButtonGroup.NewValue;
new = obj.SelectedObject.String;
switch new
case 'Transfer Path'
% display the transfer path
imagesc(app.curImage,'Parent',app.DisplayPath);
app.DisplayPath.Visible = 'off';
case 'Store'
% display the store text
text(curStore,'Parent',app.DisplayPath);
case 'Mill'
% Display the Mill text
text(curMill,'Parent',app.DisplayPath);
end
end
The part of that defined in class is as :
methods (Access = private)
% Create UIFigure and components
createComponents(app);
% Callback function for Control Button Group Selection Changed
controlBtnGroupSelectionChanged(obj, eventData, app);
end
And The part related to that in createComponents function is:
app.ControlButtonGroup = uibuttongroup(app.UIFigure);
app.ControlButtonGroup.Title = 'Control';
app.ControlButtonGroup.FontSize = 16;
app.ControlButtonGroup.Units = 'Normalized';
app.ControlButtonGroup.Position = [0.45 0.75 0.45 0.2];
app.ControlButtonGroup.SelectionChangedFcn = {@controlBtnGroupSelectionChanged,app};
Why does it say that my function is undefined?
UPDATE: I have gone through some other demos, and changed the callback as app.ControlButtonGroup.SelectionChangedFcn = @(h, e)controlBtnGroupSelectionChanged(app). And define(also prototype) it just as controlBtnGroupSelectionChanged(app). It works then. so I guess the problem should be about the number of parameters, but I still don't find much detailed information on that. Any hints are appreciated!!
Upvotes: 1
Views: 1203
Reputation: 65430
You actually need to supply the object as the first argument to the function so that MATLAB knows to look for it as a method of app
.
app.ControlButtonGroup.SelectionChangedFcn = @(s,e)controlBtnGroupSelectionChanged(app, s, e);
% Or this way which implicitly passes the two input parameters
app.ControlButtonGroup.SelectionChangedFcn = @app.controlBtnGroupSelectionChanged;
Then your method would look like this
function controlBtnGroupSelectionChanged(obj, source, event)
If you don't need the source
(the handle to the control button group) or event
(the eventdata associated with the selection event), then you can have your callback "ignore" these second and third inputs by doing something like
app.ControlButtonGroup.SelectionChangedFcn = @(s,e)app.controlBtnGroupSelectionChanged();
And your method would look like
function controlBtnGroupSelectionChanged(app)
Upvotes: 1