How to get output object name created by a method in caller workspace inside the method
Mostra commenti meno recenti
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 Commenti
per isakson
il 12 Mag 2018
Modificato: per isakson
il 12 Mag 2018
"I'm looking for the equivalent of function inputname() for output." AFAIK, there is no such function. However, I think it straightforward to make one.
- nargout returns the number of outputs
- dbstack provides the name and the line number of the caller
- fileread together with regexp provide the names of the output arguments.
and finally
- sas = evalin('caller','whos') tells whether the outputs exists
Jan
il 12 Mag 2018
There is not outputname and there cannot be one for theoretical reasons. But it is a bad idea to communicate over side-channels with the calling function. If this is really needed, what about providing the information through the inputs?
function zo = update(obj, zi, zo)
if nargin < 3 || isempty(zo)
...
I don't see why it is required to mess around with output names, nor would this actually help your given example: somehow (and it will not be simple or efficient) determining the name of the output argument does not tell you if that variable has already been defined previously (which is apparently your stated aim). So your approach is likely a red herring anyway.
Personally I would go for a much simpler option in the caller workspace, something like this:
flag = true;
...
if flag
Y = define_object(...);
flag = false;
end
Using a flag will be much more efficient than using exist. You can also easily pass the flag as an input/output argument.
Walter Roberson
il 14 Mag 2018
Please do not close questions that have an answer
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Argument Definitions in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!