MATLAB: How to determine all the objects of a particular class from the base workspace in MATLAB

MATLAB

I have a custom classdef class that I work with frequently. In one of the functions say the overloaded functions, such as save() and load() for this class. I would like to be able to determine the class object's workspace name.
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.

Best Answer

In order to do this you can write a simple function that would evaluate all the variables in the base workspace and then compare the class to which they belong. Once you find the desired classname you can return the name of that variable as a string.
Here is the function that shows how this could be done:
function out = get_Objects_From_Base(classname)
baseVariables = evalin('base' , 'whos')
out = cell(0);
for i = 1:length(baseVariables)
if (strcmpi(baseVariables(i).class , classname)) % compare classnames
out{length(out) +1} = baseVariables(i).name;
end
end
end
The function takes in an input class name and returns the name of ALL objects that are of that class from the base workspace. The example usage below will return a cell array containing the string 'objA1'
>> objA1 = myclassA;
>> get_Objects_From_Base('myclassA')