How to identify a calling function inside a called function?

23 ビュー (過去 30 日間)
Biraj Khanal
Biraj Khanal 2022 年 3 月 11 日
回答済み: Walter Roberson 2023 年 10 月 20 日
function func3(app)
app.ApplyButtonPushed();
end
end
% Callbacks that handle component events
methods (Access = private)
% Callback function: AbsoluteEditField, ApplyButton,
% RelativeEditField
function ApplyButtonPushed(app, event)
app.EditField.Value = (event.Source.Tag) %% this does not work
if event.source = app.EditField..... %% this works
if event.source = app.func3..... %this does not work
end
% Value changed function: EditField_2
function EditField_2ValueChanged(app, event)
value = app.EditField_2.Value;
app.func3(event);
end
end
Here is the idea :
editing a textbox calls a function that subsequently calls another function (like a pushed button here). I am not sure how I can execute this and more importantly, how I can know from where the button pushed function was called.
For editfields, the event.Source would have the information, but when called from a function like func3, there is no such information.
How do I access the source or name of the function inside the buttonpushed function in this case??
  2 件のコメント
Walter Roberson
Walter Roberson 2022 年 3 月 11 日
app.ApplyButtonPushed();
When you do that, how is the function getting its event parameter?
Biraj Khanal
Biraj Khanal 2022 年 3 月 16 日
編集済み: Biraj Khanal 2022 年 3 月 16 日
Can be done two ways :
Inside the value change function of app.EditField , i could call
app.ApplyButtonPushed(event);
what I have done here is simply used the existing callback of the button pushed with the app.EditField which apparently sends the event as parameter.
The problem is I can't do:
function func3(app)
app.ApplyButtonPushed(event);
end
Or something to that effect so that I could identify inside the ApplyButtonPushed function where it is triggered from. I want to know that func3 is where the ApplyButtonPushed is called from.
And thanks for pointing out the confusion. I did not include the previous step.

サインインしてコメントする。

回答 (2 件)

aditi bagora
aditi bagora 2023 年 10 月 20 日
Hello Biraj,
I understand that you are looking to determine the calling function within your ButtonPushedFcn callback function.
To identify the source of the triggered event:
  • If the source function is a triggered event, you can pass ‘eventData.Source as a parameter to your ButtonPushedFcn function.
  • If the source does not contain event information, you can pass null as a parameter.
To find the name of the calling function, you can use the dbstack() function provided by MATLAB. This function returns the function call stack.
The first function in the stack represents the called function, while the second function in the stack indicates the caller.
To extract details of the caller from the stack object, you can access the 'name' field of the stack at the second position, like this:
stack = dbstack();
calling_fn_name = stack(2).name;
disp(calling_fn_name);
Please refer the following documentation for more details on “dbstack()”
Hope this helps!
Regards,
Aditi

Walter Roberson
Walter Roberson 2023 年 10 月 20 日
See gcbo

カテゴリ

Help Center および File ExchangeHistorical Contests についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by