Preliminary documentation for Ice for MATLAB. Do not use in production applications. Refer to the space directory for other releases.

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

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:

Slice
struct Employee
{
    long number;
    string firstName;
    string lastName;
}

The MATLAB mapping generates the following definition for this structure:

MATLAB
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:

Slice
class Photo
{
    ...
}
 
struct Employee
{
    long number;
    string firstName;
    string lastName;
    Photo avatar;
}

The MATLAB mapping generates a handle class for this structure:

MATLAB
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 TypeDefault Value
stringEmpty string
enumFirst enumerator in enumeration
structDefault-constructed value
NumericZero
boolfalse
sequenceEmpty array
dictionaryInstance of the mapped type
classEmpty array

You can also declare different default values for members of primitive and enumerated types.

See Also
  • No labels