Passing Arguments to a UIMenu selected Function
Show older comments
Hello, I am creating a uimenu option on a figure that contains plots so I can scale the ylimits as I wish (i.e. force min ylim to be zero)
My plots are on axes componentns such as:
figure
ax1=subplot(1,2,1);
ax2=subplot(1,2,2);
plot(ax1,data(:,1),data(:,3),'*-','Linewidth',1'); grid(ax1,'on');
plot(ax2,data(:,1),data(:,4),'*-','Linewidth',1'); grid(ax2,'on');
app.myax2=ax2; % Assign required axes, ax2, the the app property myax2 (so to be visible in an functions)
And I create the uimenu like this:
m=uimenu('Text','YLim Options');
mitem1=uimenu(m,'Text','YLim Zero');
mitem1.MenuSelectedFcn = @app.YlimZero
where the function YlimZero is:
function YlimZero(app, ~, ~)
ax=app.myax2;
ylim(ax,[0,inf])
end
The way I decide which axes to apply to is to define myax1, myax2 etc as app properties and access them directly in the function. However, to be generic, I want to be able to apply all this to any figure with any number of plots (and axes components, ax) - so my question is, is there a way to pass the axes I want to apply to in the menu selected function, mitem1.MenuSelectedFcn = @app.YlimAuto
(sometimes I want to apply it for example to ax1 and ax3 only and leave ax2 alone)
Thanks
Jason
Accepted Answer
More Answers (2)
s pernot
on 28 Jan 2022
0 votes
hi jason
you have several options - see y. altman undocumented site for further details
here are my preferred way to solve this by decreasing preference order
- create in the main function a handle struct where you attach all requires axes handles like
handles = struct;
handles.ax1 = AxesHdl1;
handles.ax2= AxesHdl2;
% you then pass this handle structure as an argument of your YlimZero callback like
mitem1.MenuSelectedFcn = {@app.YlimZero, handles};
% then beware of the definition of your callback : it should be something like
function YlimZero(hObject, event, handles)
% hObject = handle to uimenu object
% event = event message if you want to use it (or not)
% then your handles struct
% do something .... like
ylim(handles.ax1,[0,inf])
end
2 instead of passing handle struct in argument of callback, you store it and attach it to you uimenu object in the main func
like :
setappdata(mitem1, 'handles', handles);
mitem1.MenuSelectedFcn = @app.YlimZero;
%then in you callback function
function YlimZero(app, hObject, event)
handles = getappdata(hObject, 'handles');
ylim(handles.ax1,[0,inf])
end
3. less nice : you attach handle struct to mitems UserData like :
mitem1.UserData.handles = handles;
% and so on
now go to your code, old chap
best regards
Stephane
s pernot
on 28 Jan 2022
0 votes
it also works similaly with appdesigner...
in the latter case, you can simply attach handle struct as a app property
you declare it like in the app class
properties
handles
end
% in the startupFcn
app.handles = struct;
app.handles.ax1 = AxesHdl1;
app.handles.ax2= AxesHdl2;
% and you simply use :
mitem1.MenuSelectedFcn = @app.YlimZero
function YlimZero(app, hObject, event)
% hObject = handle to uimenu object
% event = event message if you want to use it (or not)
% then your handles struct
% do something .... like
ylim(app.handles.ax1,[0,inf])
end
% that's it
Categories
Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!