MATLAB® automatically converts data from a .NET object into these MATLAB types. These values are displayed in a method signature.
C# .NET Type | MATLAB Type |
---|---|
System.Int16 | int16 scalar |
System.UInt16 | uint16 scalar |
System.Int32 | int32 scalar |
System.UInt32 | uint32 scalar |
System.Int64 | int64 scalar |
System.UInt64 | uint64 scalar |
System.Single | single scalar |
System.Double | double scalar |
System.Boolean | logical scalar |
System.Byte | uint8 scalar |
System.Enum | enum |
System.Char | char |
System.Decimal | System.Decimal |
System.Object | System.Object |
System.IntPtr | System.IntPtr |
System.UIntPtr | System.UIntPtr |
System.String | System.String |
System.Nullable<ValueType> | System.Nullable<ValueType> |
System.Array | |
System.__ComObject | |
class name | class name |
struct name | struct name |
To convert elements of a .NET array to an equivalent MATLAB array, call these MATLAB functions. For an example, see Convert Primitive .NET Arrays to MATLAB Arrays.
To convert a System.String
object to a MATLAB string, use the string
function. To convert a
System.String
object to a MATLAB character array, use the char
function. For example:
str = System.String('create a System.String');
mlstr = string(str)
mlchar = char(str)
mlstr = "create a System.String" mlchar = 'create a System.String'
MATLAB displays the string value of System.String
objects, instead
of the standard object display. For example, type:
a = System.String('test') b = System.String.Concat(a,' hello',' world')
a = test b = test hello world
The System.String
class illustrates how MATLAB handles fields and properties, as described in Call .NET Properties That Take an Argument. To see reference information about the
class, search for the term System.String
in the .NET Framework Class
Library, as described in To Learn More About the .NET Framework.
The string
function converts String.String
arrays
(String.String[]
, String.String[,]
, etcetera) to
MATLAB
string
arrays with the same dimensions and sizes. Conversion of jagged
arrays, for example String.String[][]
, is not supported.
The System.__ComObject
type represents a Microsoft® COM object. It is a non-visible, public class in the mscorlib
assembly with no public methods. Under certain circumstances, a .NET object returns an
instance of System.__ComObject
. MATLAB handles the System.__ComObject
based on the return types
defined in the metadata.
If the return type of a method or property is strongly typed, and the result of the
invocation is System.__ComObject
, MATLAB automatically converts the returned object to the appropriate type.
For example, suppose that your assembly defines a type, TestType
, and
provides a method, GetTestType
, with the following signature.
Return Type | Name | Arguments |
---|---|---|
NetDocTest.TestType RetVal | GetTestType | (NetDocTest.MyClass this) |
The return type of GetTestType
is strongly typed and the .NET
Framework returns an object of type System.__ComObject
. MATLAB automatically converts the object to the appropriate type,
NetDocTest.TestType
, shown in the following pseudo-code:
cls = NetDocTest.MyClass; var = GetTestType(cls)
var = TestType handle with no properties.
If the return type of a method or property is System.Object
, and the
result of the invocation is System.__ComObject
, MATLAB returns System.__ComObject
. To use the returned object,
cast it to a valid class or interface type. Use your product documentation to identify the
valid types for this object.
To call a member of the new type, cast the object using the MATLAB conversion syntax:
objConverted = namespace.classname(obj)
where obj
is a System.__ComObject
type.
For example, an item in a Microsoft
Excel® sheet collection can be a chart or a worksheet. The following command converts
the System.__ComObject
variable mySheet
to a
Chart
or a Worksheet
object
newSheet
:
newSheet = Microsoft.Office.Interop.Excel.interfacename(mySheet);
where interfacename
is Chart
or
Worksheet
. For an example, see Work with Microsoft Excel Spreadsheets Using .NET.
If you pass a COM object to or from a function, lock the object so that MATLAB does not automatically release it when the object goes out of scope. To lock
the object, call the NET.disableAutoRelease
function. Then unlock
the object, using the NET.enableAutoRelease
function, after you are
through using it.
If .NET returns a System.Nullable
type, MATLAB returns the corresponding System.Nullable
type.
A System.Nullable
type lets you assign null
values
to types, such as numeric types, that do not support null
value. To use a
System.Nullable
object in MATLAB, first decide how to handle null
values.
If you want to process null
values differently from
<ValueType>
values, use the HasValue
property.
If you want every value to be of the underlying <ValueType>
,
use the GetValueOrDefault
method. This method assigns a default value
of type <ValueType>
to null
values.
Use a variable of the object's underlying type where appropriate in any MATLAB expression. For examples, see Pass System.Nullable Arguments.
MATLAB handles dynamic types as System.Object
. For example, the
following C# method exampleMethod
has a dynamic input argument
d
and returns a dynamic output value:
public dynamic exampleMethod(dynamic d)
The following table shows the corresponding MATLAB function signature.
Return Type | Name | Arguments |
---|---|---|
System.Object RetVal | exampleMethod | ( |
You must convert a .NET jagged array before using it in a MATLAB command. To convert:
If the shape of the array is rectangular, use the corresponding MATLAB numeric function.
If the array is not rectangular, use the cell
function.
If the jagged array is multidimensional, you must individually convert the arrays in each dimension.