How to get output object name created by a method in caller workspace inside the method

2 views (last 30 days)
In order to avoid overwriting the object when the property of that object gets an update, the output object only needs to be constructed when it doesn't exist in the caller workspace.
function zo = update(obj, zi)
% obj is an object of a class; while zi & zo are objects of the same class
% different than obj.
if ~exist(zo) % if zo doesn't exist already in parent workspace
zo = const(); % construct object zo
end
zo = func(zi)
end
In the parent workspace, responding to an event of A, an object Y updates its property based on object X.
Y = A.update(X)
So, inside the function (method) definition, the line
if ~exist(zo)
should have been
if ~exist(Y)
How do I know what name (like "Y") the user have assigned "zo" to from within function "update"? In another word, I'm looking for the equivalent of function inputname() for output.
  4 Comments

Sign in to comment.

Accepted Answer

Jan
Jan on 12 May 2018
This sounds like meta-programming: Using an indirect way around the standard channels to provide information. This would work e.g. by parsing the source code of the calling function. But this is far too complicated and prone to errors. Therefore I'm convinced that the best approach to solve your needs is to chose a different design. Provide the required data as inputs instead of digging in the callers workspace.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!