Callback function not found for dynamically created app UI component

3 次查看(过去 30 天)
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 个评论
Jan
Jan 2021-3-20
Please post code as text, such that writing an answer allows to copy&paste.
Monika Jaskolka
Monika Jaskolka 2021-3-20
编辑:Monika Jaskolka 2021-3-20
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

请先登录,再进行评论。

采纳的回答

Monika Jaskolka
Monika Jaskolka 2021-3-25
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)".

更多回答(2 个)

Jan
Jan 2021-3-20
编辑:Jan 2021-3-20
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 个评论
Monika Jaskolka
Monika Jaskolka 2021-3-20
编辑:Monika Jaskolka 2021-3-20
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);

请先登录,再进行评论。


Bear
Bear 2021-11-15
I have a similar issue.
I'm creating a submenu voice with a file path and I want to associate a function. The idea is the add a voice every time the user selects a directory in order to have a history of selected folders.
I would like to catch also the event when the user selects a folder in order to retrieve the selected path and move to that folder.
mitem = uimenu(app.RecentpathsMenu,"Text",user_dir,"MenuSelectedFcn",@(~,~)app.ChangePath);

类别

Help CenterFile Exchange 中查找有关 Develop uifigure-Based Apps 的更多信息

产品


版本

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by