Passing Arguments to a UIMenu selected Function

11 views (last 30 days)
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

Jason
Jason on 29 Jan 2022
I think I have a solution, I can pass which ever axes (or number of axes) to myax as follows
app.myax(1)=ax1;
app.myax(2)=ax2;
myax is a public property of my app.
Then in the YlimZero function
function YlimZero(app, ~, ~)
n=numel(app.myax); % Determin how many axes have been passed
for i=1:n
ylim(app.myax(i),[0,inf]) % Adjust the Axes as required
end
end
  1 Comment
s pernot
s pernot on 29 Jan 2022
god bless you.
Now you realize you are great at helping yourself
congratulations

Sign in to comment.

More Answers (2)

s pernot
s pernot on 28 Jan 2022
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
  1. 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
  1 Comment
Jason
Jason on 28 Jan 2022
Hi, thanks for your fast reply. I think your solution is for GUIDE as its using handles and appdata. Im using appdesigner so they won't work.

Sign in to comment.


s pernot
s pernot on 28 Jan 2022
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
  2 Comments
Jason
Jason on 29 Jan 2022
But Im still having to decide which ax is used in the YlimZero function - this is what I'm trying to avoid. I want to select which axes to use (sometimes more than 1) in the calling function, so I can use YlimZero generically for any figures I create with this menu option.
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
s pernot
s pernot on 29 Jan 2022
sorry for this, i am not in your head... you have to be creative
that is your own job now to achieve this... the is the real work before coding
to imagine how to make a suitable design...
Good luck

Sign in to comment.

Categories

Find more on Specifying Target for Graphics Output in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!