MATLAB Object-Oriented Programming

(Joyce) #1

ans =
SubInt


Use the strcmp function with the class function to check for a specific class of an
object:


a = int16(7);
strcmp(class(a),'int16')


ans =
1


Because the class function returns the class name as a char vector, the inheritance
does not affect the result of the comparison performed by strcmp:


aInt = SubInt;
strcmp(class(aInt),'int16')


ans =
0


Test for Most Derived Class


If you define functions that require inputs that are:



  • MATLAB built-in types

  • Not subclasses of MATLAB built-in types


Use the following techniques to exclude subclasses of built-in types from the input
arguments.



  • Define a cell array that contains the names of built-in types accepted by your function.

  • Call class and strcmp to test for specific types in a MATLAB control statement.


Test an input argument:


if strcmp(class(inputArg),'single')
% Call function
else
inputArg = single(inputArg);
end


Determine Array Class
Free download pdf