class - Create object or return class of object

Syntax

str = class(object)
obj = class(s,'class_name')
obj = class(s,'class_name',parent1,parent2,...)
obj = class(struct([]),'class_name',parent1,parent2,...)
obj_struct = class(struct_array,'class_name',parent_array)

Description

str = class(object) returns a string specifying the class of object.

The following table lists the class names that can be returned. All except the last one are MATLAB classes.

logical

Logical array of true and false values

char

Character array

int8

8-bit signed integer array

uint8

8-bit unsigned integer array

int16

16-bit signed integer array

uint16

16-bit unsigned integer array

int32

32-bit signed integer array

uint32

32-bit unsigned integer array

int64

64-bit signed integer array

uint64

64-bit unsigned integer array

single

Single-precision floating-point number array

double

Double-precision floating-point number array

cell

Cell array

struct

Structure array

function_handle

Array of values for calling functions indirectly

'class_name'

User–defined MATLAB® class

'Java_class_name'

Java™ class

Using the class function within a class constructor (pre MATLAB Version 7.6)

The following usage of the class function is restricted to pre MATLAB Version 7.6 class constructors (classes defined without a classdef statement). It can be used only within a function named class_name.m, which is in a directory named @class_name (where class_name is the same as the string passed to class and is the name of the class being constructed).

See Class Constructor Methods for information on implementing class constructor methods in MATLAB Version 7.6 and after.

obj = class(s,'class_name') creates an object of class class_name using the struct s as a pattern to determine the size of obj.

obj = class(s,'class_name',parent1,parent2,...) creates an object of class class_name that inherits the methods and fields of the parent objects parent1, parent2, and so on. The struct s is used as a pattern to determine the size of obj. The size of the parent objects must match the size of s or be a scalar (1–by-1), in which case, MATLAB performs scalar expansion.

obj = class(struct([]),'class_name',parent1,parent2,...) creates an object of class class_name that inherits the methods and fields of the parent objects parent1, parent2, and so on. Specifying the empty structure struct([]) as the first argument ensures that the object created contains no fields other than those that are inherited from the parent objects. All parents must have the same, nonzero size, which determines the size of the returned object obj.

Arrays of objects

obj_struct = class(struct_array,'class_name',parent_array) struct_array is an array of structs and parrent_array is an array of parent objects. Every element of the parrent_array is mapped to a corresponding element in the struct_array to produce the output array of objects, obj_struct. All arrays must be of the same size or, if either the struct_array or the parrent_array is of size 1–by1, then MATLAB performs scalar expansion to match the array sizes.

Note that you can create an object array of size 0–by-0 by setting the size of the struct_array and parrent_array to 0–by-0.

Examples

To return in nameStr the class of Java object j,

nameStr = class(j)

Obtain the full name of a package-based Java class,

import java.lang.*;
obj = String('mystring');
class(obj)

See Also

inferiorto, isa, struct, superiorto

MATLAB® Classes and Object-Oriented Programming

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS