How to get the class instance 'calling' path
古いコメントを表示
Hi all
I'm working on a class and I have added an error() message to a part of the code. However within my model several instances of the class will be available and I would like to know which instance is throwing the error. How can I find the instance 'calling' path (see red rectangle) within the class itself?

採用された回答
その他の回答 (2 件)
Pratyush
2023 年 10 月 25 日
Hi Ludo,
I understand that you have added an error message in your class and you want to know which instance of the class has generated the error.
In MATLAB, you can use the "dbstack" function to obtain the call stack information, including the calling path within the class. You can refer to the following documentation for more information: https://in.mathworks.com/help/matlab/ref/dbstack.html
Here is an example how "dbstack" could be used to get the instance that throws the error:
classdef MyClass
properties
name
end
methods
function obj = MyClass(name)
obj.name = name;
end
function error(obj)
stack = dbstack();
calling_path = stack(1);
disp(['Error occurred in instance ''' obj.name ''' at line ' num2str(calling_path.line) ' in file ' calling_path.file]);
end
end
end
Hope this helps.
Ludo Houben
2023 年 10 月 26 日
カテゴリ
ヘルプ センター および File Exchange で Simulink Environment Customization についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!