Call component callback from another function
16 ビュー (過去 30 日間)
古いコメントを表示
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 ...
end
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.
0 件のコメント
採用された回答
その他の回答 (1 件)
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 件のコメント
Yongjian Feng
2022 年 1 月 18 日
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 Center および File 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!