Can't get 'ValueChangedFcn' callback to work for 'uidatepicker '

2 次查看(过去 30 天)
Hi, I'm would like to have a date picker window on my main app window, and I'd like something to happen whenever the date is changed. The help file says I can call a function using the 'ValueChangedFcn' callback, but I can't get it to work. This is the relevant piece of code: methods (Access = private)
function test(app)
uialert(app.UIFigure, 'It works!', 'It works!', 'Icon','help');
end
end
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
uidatepicker(app.UIFigure, 'Position', [780 350 100 22], 'ValueChangedFcn', test(app))
I get this error message - Error using app1/test Too many output arguments.
Error in app1 (line 431) runStartupFcn(app, @startupFcn)
Any ideas?

回答(1 个)

Abhipsa
Abhipsa 2025-1-30
Hi, I understand that you want to add a date picker to the main app window and trigger an action whenever the date is changed using “ValueChangedFcn” callback function.
This issue can be resolved by using a function handle for the callback instead of directly invoking the function in the “ValueChangedFcn”. Additionally, confirm that the callback function is designed to accept the necessary input arguments.
methods (Access = private)
function test(app, src, event)
uialert(app.UIFigure, 'It works!', 'It works!', 'Icon', 'info'); % I have changed 'help' to 'info' here
end
end
% You can use the below code in runStartupFcn
uidatepicker(app.UIFigure, ...
'Position', [100 100 150 22], ...
'ValueChangedFcn', @(src, event) test(app, src, event));
Moreover, "help" used for the value of "Icon" parameter might not be valid, if it is not created explicitly, you can also use an icon from the set of predefined icons provided by MATLAB.
You can also refer to the below documentations for more details:

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by