There are two mappings for Slice structures, depending on whether they contain class data members:
- A structure that does not contain class data members maps to a MATLAB value type
- A structure that contains class data members maps to a MATLAB handle type
The mappings are nearly identical, but you need to be aware of the differences because they behave differently at run time in some situations.
On this page:
Value Mapping for Structures
A Slice structure maps to a MATLAB value class containing a public property for each member of the structure. For example, here is our Employee
structure once more:
struct Employee { long number; string firstName; string lastName; }
The MATLAB mapping generates the following definition for this structure:
classdef Employee properties number int64 firstName char lastName char end methods function obj = Employee(number, firstName, lastName) ... end end ... end
The class provides a constructor whose optional arguments correspond to the data members. This allows you to instantiate and initialize the class in a single statement (instead of having to first instantiate the class and then assign to its members). You must either call the constructor with no arguments or with arguments for all of the data members.
Handle Mapping for Structures
Suppose we modify the Employee
structure to add a class data member:
class Photo { ... } struct Employee { long number; string firstName; string lastName; Photo avatar; }
The MATLAB mapping generates a handle class for this structure:
classdef Employee < matlab.mixin.Copyable properties number int64 firstName char lastName char avatar end methods function obj = Employee(number, firstName, lastName, avatar) ... end ... end end
The class derives from matlab.mixin.Copyable
, which derives from handle
. The constructor accepts optional arguments corresponding to the data members, allowing you to instantiate and initialize the class in a single statement (instead of having to first instantiate the class and then assign to its members). You must either call the constructor with no arguments or with arguments for all of the data members.
Comparing Value and Handle Semantics
A structure that contains one or more class data members must be mapped as a handle class to support the Ice run time's unmarshaling requirements. As you may already know, MATLAB treats value types differently than handle types when passing them as parameters: value types are passed by value (i.e., copied) while handle types are passed by reference. Please keep this difference in mind while developing your application.
Regardless of whether a structure is mapped as a value type or a handle type, instances can be compared using the isequal
function and copied using the copy
function.
Default Property Values
Calling the generated constructor with no arguments assigns a default value appropriate for each member's type:
Data Member Type | Default Value |
---|---|
string | Empty string |
enum | First enumerator in enumeration |
struct | Default-constructed value |
Numeric | Zero |
bool | false |
sequence | Empty array |
dictionary | Instance of the mapped type |
class | Empty array |
You can also declare different default values for members of primitive and enumerated types.