If expression to check whether a particular function has been called

32 ビュー (過去 30 日間)
Tejas
Tejas 2020 年 8 月 6 日
コメント済み: Tejas 2020 年 8 月 6 日
What should I write in the expression for the 'if' statement when I want to check if the script has called a particular function, and if it has, then perform the calculations mentioned?
  2 件のコメント
Roger J
Roger J 2020 年 8 月 6 日
Maybe set a global variable (a flag) inside of the function. Like in foo() the first line can set fooHasBeenCalled=1
Then you can use
if fooHasBeenCalled==1
% code
end
Tejas
Tejas 2020 年 8 月 6 日
That works for me. Thanks!

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

採用された回答

Walter Roberson
Walter Roberson 2020 年 8 月 6 日
In the general case, there is no way to detect whether a particular function has been called.
If you turn on profiling before you execute, then you can potentially examine the profile information to determine whether the function was called.
If the function happens to change state in a distinct way, such as creating a global variable, or opening a particular file, then you might be able to examine the program state to see if the function "must have been" called.
If the function is a .m file, then you could potentially put in a breakpoint in the file in order to determine whether the function was reached.
Usually the easiest way to deal with such matters is to create a flag variable that is initialized to false, with the program setting the flag to true immediately after calling the function. This requires the cooperation of the function.
  1 件のコメント
Tejas
Tejas 2020 年 8 月 6 日
Yes, a flag variable works for me. Thanks!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by