MATLAB: Any way to get the “current” class

classMATLABoop

Hi,
I am writing some object oriented code (with classdef), and it would be convenient for me to have a function like this:
meta.class.getCurrent()
Where it returns the metadata of the class in which it is called. In the event that the function is called from some other place (script or simple function) it should error
Is there a way to get that?
Thanks, Joan

Best Answer

This might be a start. (TODO: not a class, class in package, function in package.)
>> MyTestClass.Update
Class_name: MyTestClass
Method_name: Update
where
classdef MyTestClass < handle
methods ( Static )
function Update()
[ cls, method ] = current_class();
fprintf( 'Class_name: %s\nMethod_name: %s\n', cls.Name, method )
end
end
end
and
function [ cls, str ] = current_class( )
%
% See also: mfilename
dbk = dbstack( 1, '-completenames' );
if isempty( dbk )
str = 'base';
else
str = dbk(1).name;
end
cac = regexp( str, '\.', 'split' );
switch numel( cac )
case 1
case 2
cls = meta.class.fromName( cac{1} );
str = cac{2};
case 3
otherwise
end
end