Help with undefined function error in context menu in App Designer?
23 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Dan Zabetakis
am 27 Sep. 2023
Kommentiert: Voss
am 27 Sep. 2023
I'm having a problem getting context menues to work in a Matlab App. I'm unclear on the exact usage and examples and questions in the community haven't helped. It is apparently not seeing my Menu Selected Function. The same code works in a standard figure, but fails in the app.UIFigure. I'm trying to create context menus associated with specific plots in the UIAxes.
The error is "Undefined function 'Line1' for input arguments of type 'matlab.ui.container.Menu'.
Error using matlab.ui.internal.controller.WebMenuController/fireActionEvent
Error while evaluating Menu Callback."
The code is:
classdef TestApp1 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
DropDown matlab.ui.control.DropDown
DropDownLabel matlab.ui.control.Label
UIAxes matlab.ui.control.UIAxes
end
methods (Access = private)
function Line1(app,src,event,K)
disp('This is Line 1')
disp(src)
disp(event)
disp(K)
end
function Line2(app,src,event)
disp('This is Line 2')
end
end
% Callbacks that handle component events
methods (Access = private)
% Value changed function: DropDown
function DropDownValueChanged(app, event)
CM=uicontextmenu(app.UIFigure);
M1=uimenu(CM,"Text","Line1");
M1.MenuSelectedFcn = {@Line1, 3};
M2=uimenu(CM,"Text","Line2");
M2.MenuSelectedFcn = @Line2;
value = app.DropDown.Value;
P1=plot(app.UIAxes,[1 2],[1 1],'r-',"ContextMenu",CM);
hold(app.UIAxes,'on')
P2=plot(app.UIAxes,[1 3],[2 2],'b-',"ContextMenu",CM);
ylim(app.UIAxes,[0 3]);
xlim(app.UIAxes,[0 4]);
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = 'MATLAB App';
% Create UIAxes
app.UIAxes = uiaxes(app.UIFigure);
title(app.UIAxes, 'Title')
xlabel(app.UIAxes, 'X')
ylabel(app.UIAxes, 'Y')
zlabel(app.UIAxes, 'Z')
app.UIAxes.Position = [127 68 387 321];
% Create DropDownLabel
app.DropDownLabel = uilabel(app.UIFigure);
app.DropDownLabel.HorizontalAlignment = 'right';
app.DropDownLabel.Position = [45 427 65 22];
app.DropDownLabel.Text = 'Drop Down';
% Create DropDown
app.DropDown = uidropdown(app.UIFigure);
app.DropDown.ValueChangedFcn = createCallbackFcn(app, @DropDownValueChanged, true);
app.DropDown.Position = [125 427 100 22];
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = TestApp1
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
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
0 Kommentare
Akzeptierte Antwort
Voss
am 27 Sep. 2023
Use "@app.Line1" and "@app.Line2" instead of "@Line1" and "@Line2", since those functions are app methods.
See below:
classdef TestApp1 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
DropDown matlab.ui.control.DropDown
DropDownLabel matlab.ui.control.Label
UIAxes matlab.ui.control.UIAxes
end
methods (Access = private)
function Line1(app,src,event,K)
disp('This is Line 1')
disp(src)
disp(event)
disp(K)
end
function Line2(app,src,event)
disp('This is Line 2')
end
end
% Callbacks that handle component events
methods (Access = private)
% Value changed function: DropDown
function DropDownValueChanged(app, event)
CM=uicontextmenu(app.UIFigure);
M1=uimenu(CM,"Text","Line1");
M1.MenuSelectedFcn = {@app.Line1, 3};
M2=uimenu(CM,"Text","Line2");
M2.MenuSelectedFcn = @app.Line2;
value = app.DropDown.Value;
P1=plot(app.UIAxes,[1 2],[1 1],'r-',"ContextMenu",CM);
hold(app.UIAxes,'on')
P2=plot(app.UIAxes,[1 3],[2 2],'b-',"ContextMenu",CM);
ylim(app.UIAxes,[0 3]);
xlim(app.UIAxes,[0 4]);
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = 'MATLAB App';
% Create UIAxes
app.UIAxes = uiaxes(app.UIFigure);
title(app.UIAxes, 'Title')
xlabel(app.UIAxes, 'X')
ylabel(app.UIAxes, 'Y')
zlabel(app.UIAxes, 'Z')
app.UIAxes.Position = [127 68 387 321];
% Create DropDownLabel
app.DropDownLabel = uilabel(app.UIFigure);
app.DropDownLabel.HorizontalAlignment = 'right';
app.DropDownLabel.Position = [45 427 65 22];
app.DropDownLabel.Text = 'Drop Down';
% Create DropDown
app.DropDown = uidropdown(app.UIFigure);
app.DropDown.ValueChangedFcn = createCallbackFcn(app, @DropDownValueChanged, true);
app.DropDown.Position = [125 427 100 22];
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = TestApp1
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
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
2 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Develop Apps Using App Designer finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!