Call component callback from another function

조회 수: 15 (최근 30일)
Goncalo Torres
Goncalo Torres 2022년 1월 18일
답변: Goncalo Torres 2022년 1월 25일
Hello all,
I am in a situation where I need to call a component's callback from another function. This component (a file menu) and its callback are defined in another script as follows (I am not 100% if this is the most efficient way to define callbacks but it's the way I've been using):
% "file1.m"
% Create SaveMenu
app.GUI.Menus.menuSave = uimenu(app.GUI.Menus.menuFile);
app.GUI.Menus.menuSave.MenuSelectedFcn = {@Save, app};
app.GUI.Menus.menuSave.Text = 'Save';
function Save(~, ~, app)
% ... do something ...
I need to call the function named Save from another script (let's call it "file2.m"). Is there anyway I can refer to the component (app.GUI.Menus.menuSave) and execute its MenuSelectedFcn? I have tried a couple of different ways but none of them works...
Thank you.

채택된 답변

Goncalo Torres
Goncalo Torres 2022년 1월 25일
Found a solution for it.
Basically, using feval, I can pass the function callback as follows:

추가 답변 (1개)

Yongjian Feng
Yongjian Feng 2022년 1월 18일
편집: Yongjian Feng 2022년 1월 18일
A callback function in your case is just a method of a class. You called your class file1 apparently. The Save method doesn't depend on the file1 obj. So can be called by creating a temp file1 object, and then call the Save method of that file1 object.
The only thing you need to worry about is the last input argument (called app in your file). Not sure how app is used inside the Save method. You might need to find the correct app when you call Save.
  댓글 수: 2
Goncalo Torres
Goncalo Torres 2022년 1월 18일
Hi Yongjian,
Thank you for your reply.
To give you a bit more context, this is from an application, intially developed from app designer. However, I do not like to keep the code in a single mlapp file, so I normally create external functions with functionality or with component declaration. In this case, file1.m is just a function file that creates the file menu of my application, and not an object.
In file1.m, I create my component (menuSave) and assign it the function @Save for when the menu is clicked. Therefore, @save is only a sub-function in the function file "file1.m", and I cannot call it directly.
However, I do have access to my component menuSave, which containts the handle to the function @Save. I just don't know how to execute it from there.
K>> app.GUI.Menus.menuSave.MenuSelectedFcn
ans =
2×1 cell array
{ @Save} % this is the function handle to save
{1×1 app}
Yongjian Feng
Yongjian Feng 2022년 1월 18일
I see. Basically you define your Save function as local function of script file1.m.
You can't call a local function of a script outside of file1.m.
Since Save doesn't depend on anything else except the input arguments (basically it just needs app), it can be a global function.
Give it a better name (to decribe what it is). Something like SaveFileFromApp. Then put it in a file called SaveFileFromApp.m. Make sure it is accessible for both callers.

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


Help CenterFile Exchange에서 Develop Apps Using App Designer에 대해 자세히 알아보기




Community Treasure Hunt

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

Start Hunting!

Translated by