Is there a MATLAB function that can determine the workspace name for a given argument. Something like function who() but that would take a MATLAB object as an argument and return a string.
I have a custom classdef class that I work with frequently, and have overloaded the functions save() and load() for this class. I declared my save function as:
function save(obj,fileName)
and typically use the workspace variable name as (fileName), so for example:
>>myObj = MyClass; >>myObj.save('myObj') >>... >>MyClass.load('myObj') % creates (myObj) in base workspace
Ideally, I would like to be able to determine the class object's workspace name within my overloaded save() method. So for example, I would like to be able to execute the above steps as:
>>myObj1 = MyClass; >>myObj1.save >>... >>MyClass.load('myObj1') % creates (myObj1) in base workspace
I realize that I could require the object name to be specified in the class constructor, but I don't like this idea; for example:
>>myObj2 = MyClass('myObj2'); >>myObj2.save >>... >>MyClass.load('myObj2') % creates (myObj2) in base workspace
I expected to find a Matlab function call that would determine the workspace name for a given argument. Something like function who() but that would take a Matlab object as an argument and return a string. For example,
>>myObj3 = MyClass('myObj3'); >>objectName = who(myObj3,'base') % look in base workspace
ans = 'myObj3'
I could then use this function call within MyClass.save() to determine the workspace name of the class object.
Best Answer