MATLAB Answers

Callback function not found for dynamically created app UI component

12 views (last 30 days)
I would like to dynamically/programmtically create a uidropdown component that calls a function when its value is changed. However, even though I provide a function handle as the ValueChangedFcn property, the uidropdown is unable to find the function. I receive the error below.
Undefined function 'DropDownChange' for input arguments of type 'matlab.ui.control.DropDown'.
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 305)
Error while evaluating DropDown PrivateValueChangedFcn
I am attaching a simple app created in R2016b that exhibits this behavior. Why can't the function be found? How should I define it to resolve the issue?
  2 Comments
Monika Jaskolka
Monika Jaskolka on 20 Mar 2021
The code was in the attached mlapp, but here it is again:
classdef App_Test < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
end
properties (Access = private)
DropDown
end
methods (Access = public)
function DropDownChange(app)
disp('Works!')
end
end
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
app.DropDown = uidropdown(app.UIFigure, ...
'Position', [80, 100, 100 ,24], ...
'ValueChangedFcn', @DropDownChange);
end
end
% App initialization and construction
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure
app.UIFigure = uifigure;
app.UIFigure.Position = [100 100 274 165];
app.UIFigure.Name = 'UI Figure';
setAutoResize(app, app.UIFigure, true)
end
end
methods (Access = public)
% Construct app
function app = App_Test()
% Create and configure components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
% Execute the startup function
runStartupFcn(app, @startupFcn)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end

Sign in to comment.

Accepted Answer

Monika Jaskolka
Monika Jaskolka on 25 Mar 2021
I ended up contacting MathWorks support for this. The solution is to call the function like so:
app.DropDown = uidropdown(app.UIFigure, ...
'Position', [80, 100, 100 ,24], ...
'ValueChangedFcn', @(~,~)app.DropDownChange);
The first argument passed to the callback is the uidropdown, so I don't want to name it "app" and shadow the "app" argument. Additionally, since I want to invoke a method of the app, I need to call either "app.DropDownChange" or "DropDownChange(app)".

More Answers (1)

Jan
Jan on 20 Mar 2021
Edited: Jan on 20 Mar 2021
As far as I can see, Matlab searchs for a matching function with the input (app, event), but your callback accepts (app) only. So try to append a 2nd (unused...) input to your CropDownChange function.
[EDITED] No, this does not work.
  1 Comment
Monika Jaskolka
Monika Jaskolka on 20 Mar 2021
The following still gives the same error. I believe I am following the format outlined here correctly, unless I am missing something!
app.DropDown = uidropdown(app.UIFigure, ...
'Position', [80, 100, 100 ,24], ...
'ValueChangedFcn', @(app,event)DropDownChange);

Sign in to comment.

Products


Release

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by