Callback function not found for dynamically created app UI component

5 Ansichten (letzte 30 Tage)
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 Kommentare
Jan
Jan am 20 Mär. 2021
Please post code as text, such that writing an answer allows to copy&paste.
Monika Jaskolka
Monika Jaskolka am 20 Mär. 2021
Bearbeitet: Monika Jaskolka am 20 Mär. 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

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Monika Jaskolka
Monika Jaskolka am 25 Mär. 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)".

Weitere Antworten (2)

Jan
Jan am 20 Mär. 2021
Bearbeitet: Jan am 20 Mär. 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 Kommentar
Monika Jaskolka
Monika Jaskolka am 20 Mär. 2021
Bearbeitet: Monika Jaskolka am 20 Mär. 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);

Melden Sie sich an, um zu kommentieren.


Bear
Bear am 15 Nov. 2021
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);

Kategorien

Mehr zu Develop uifigure-Based Apps finden Sie in Help Center und File Exchange

Produkte


Version

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by