Passing Arguments to a UIMenu selected Function

조회 수: 7 (최근 30일)
Jason
Jason 2022년 1월 28일
댓글: s pernot 2022년 1월 29일
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

채택된 답변

Jason
Jason 2022년 1월 29일
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
s pernot
s pernot 2022년 1월 29일
god bless you.
Now you realize you are great at helping yourself
congratulations

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

s pernot
s pernot 2022년 1월 28일
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
Jason
Jason 2022년 1월 28일
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.

댓글을 달려면 로그인하십시오.


s pernot
s pernot 2022년 1월 28일
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
Jason
Jason 2022년 1월 29일
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 2022년 1월 29일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by