MATLAB Object-Oriented Programming

(Joyce) #1
Test for Category of Types

Suppose that you create a MEX-function, myMexFcn, that requires two numeric inputs
that must be of type double or single:

outArray = myMexFcn(a,b)

Define a cell array that contains the character arrays double and single:

floatTypes = {'double','single'};

% Test for proper types
if any(strcmp(class(a),floatTypes)) && ...
any(strcmp(class(b),floatTypes))
outArray = myMexFcn(a,b);
else
% Try to convert inputs to avoid error
...
end

Another Test for Built-In Types

Use isobject to separate built-in types from subclasses of built-in types. The isobject
function returns false for instances of built-in types:

% Create a int16 array
a = int16([2,5,7,11]);
isobject(a)

ans =
0

Determine if an array is one of the built-in integer types:

if isa(a,'integer') && ~isobject(a)
% a is a built-in integer type
...
end

12 How to Build on Other Classes

Free download pdf