.NET オブジェクトから返されるデータの処理
ここの表では、MATLAB® で C# .NET 型が MATLAB 型にどのようにマッピングされるのかを示します。MATLAB データ型を .NET メソッドに渡す方法については、.NET オブジェクトへのデータの受け渡しを参照してください。
.NET 型の MATLAB 型へのマッピング
MATLAB は自動的にデータを .NET オブジェクトから以下の MATLAB 型に変換します。これらの値はメソッド シグネチャに表示されます。
| C# .NET 型 | MATLAB 型 |
|---|---|
System.Int16 | int16 スカラー |
System.UInt16 | uint16 スカラー |
System.Int32 | int32 スカラー |
System.UInt32 | uint32 スカラー |
System.Int64 | int64 スカラー |
System.UInt64 | uint64 スカラー |
System.Single | single スカラー |
System.Double | double スカラー |
System.Boolean | logical スカラー |
System.Byte | uint8 スカラー |
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 | .NET アプリケーションによる配列の使用を参照してください。 |
System.__ComObject | MATLAB による System.__ComObject の処理方法を参照してください。 |
class name | class name |
struct name | struct name |
プリミティブ .NET 型の配列を MATLAB 型に変換
.NET 配列の要素を、等価な MATLAB 配列に変換するには、以下の MATLAB 関数を呼び出します。例については、MATLAB 配列へのプリミティブ .NET 型配列の変換を参照してください。
MATLAB による System.String の処理方法
System.String オブジェクトを MATLAB string に変換するには、関数 string を使用します。System.String オブジェクトを MATLAB 文字配列に変換するには、関数 char を使用します。以下に例を示します。
str = System.String('create a System.String');
mlstr = string(str)
mlchar = char(str)
mlstr =
"create a System.String"
mlchar =
'create a System.String'
MATLAB には、標準オブジェクトが表示される代わりに、System.String オブジェクトの文字列の値が表示されます。たとえば、次のように入力します。
a = System.String('test') b = System.String.Concat(a,' hello',' world')
a = test b = test hello world
System.String クラスは、引数を取る .NET プロパティの呼び出しで説明されているように、MATLAB でのフィールドとプロパティの処理方法を示します。クラスの参照情報を参照するには、.NET に関する詳細で説明されているように、.NET クラス ライブラリのドキュメンテーションで用語 System.String を検索してください。
関数 string は、String.String 配列 (String.String[]、String.String[,] など) を、同じ次元とサイズの MATLAB string 配列に変換します。String.String[][] などのジャグ配列の変換はサポートされません。
MATLAB による .NET ディクショナリ オブジェクトの処理方法
オブジェクトの関数 dictionary を呼び出すことで、System.Collections.Generic.IDictionary<,> インターフェイスを実装するオブジェクトを MATLAB ディクショナリ型に変換できます。構文は、次のようになります。
d = dictionary(netDict)
ここで、netDict は、IDictionary<> インターフェイスを実装する型をもつ .NET オブジェクトです。d のキーと値は、.NET 型の MATLAB 型へのマッピングで説明されている既定の変換ルールによって決定されます。
System.Int32 を System.Double にマッピングするジェネリックの .NET ディクショナリからの MATLAB ディクショナリの作成
System.Int32 を System.Double にマッピングする .NET Dictionary をインスタンス化します。
netDict = NET.createGeneric( "System.Collections.Generic.Dictionary", ... {"System.Int32", "System.Double"} );
3 つの要素を .NET Dictionary に追加します。
netDict.Add(1, 1.1); netDict.Add(2, 2.2); netDict.Add(3, 3.3); netDict
netDict =
Dictionary<System*Int32,System*Double> with properties:
Comparer: [1×1 System.Collections.Generic.GenericEqualityComparer<System*Int32>]
Count: 3
Keys: [1×1 System.Collections.Generic.Dictionary*KeyCollection<System*Int32,System*Double>]
Values: [1×1 System.Collections.Generic.Dictionary*ValueCollection<System*Int32,System*Double>].NET Dictionary を MATLAB ディクショナリに変換します。ここで、System.Int32 は int32 にマッピングされ、System.Double は double にマッピングされます。
d = dictionary(netDict)
d =
dictionary (int32 ⟼ double) with 3 entries:
1 ⟼ 1.1
2 ⟼ 2.2
3 ⟼ 3.3System.Char を System.String にマッピングするジェネリックの .NET ConcurrentDictionary からの MATLAB ディクショナリの作成
System.Char を System.String にマッピングする .NET ConcurrentDictionary をインスタンス化します。
netDict = NET.createGeneric( "System.Collections.Concurrent.ConcurrentDictionary", ... {"System.Char", "System.String"} );
3 つの要素を .NET ConcurrentDictionary に追加します。
netDict.Add('a', "aaa"); netDict.Add('b', "bbb"); netDict.Add('c', "ccc"); netDict
netDict =
ConcurrentDictionary<System*Char,System*String> with properties:
Comparer: [1×1 System.Collections.Concurrent.GenericEqualityComparer<System*Char>]
Count: 3
Keys: [1×1 System.Collections.Concurrent.ConcurrentDictionary*KeyCollection<System*Char,System*String>]
Values: [1×1 System.Collections.Concurrent.ConcurrentDictionary*ValueCollection<System*Char,System*String>].NET ConcurrentDictionary を MATLAB ディクショナリに変換します。ここで、System.Char は char (ディクショナリで string に変換される) にマッピングされ、System.String は System.String にマッピングされます。
d = dictionary(netDict)
d =
dictionary (string ⟼ System.String) with 3 entries:
"a" ⟼ "aaa"
"b" ⟼ "bbb"
"c" ⟼ "ccc"ディクショナリ値の型を MATLAB string に変更します。
d = dictionary( keys(d), string(values(d, "cell")) )d =
dictionary (string ⟼ string) with 3 entries:
"a" ⟼ "aaa"
"b" ⟼ "bbb"
"c" ⟼ "ccc"System.Type を System.Double[] にマッピングするジェネリックの .NET SortedDictionary からの MATLAB ディクショナリの作成
System.Type を System.Double[] にマッピングする .NET SortedDictionary をインスタンス化します。
netDict = NET.createGeneric( "System.Collections.Generic.SortedDictionary", ... {"System.Type", "System.Double[]"} );
3 つの要素を .NET SortedDictionary に追加します。
netDict.Add( System.Type.GetType("System.Int32"), [1 2 3] ); netDict.Add( System.Type.GetType("System.Double"), [4 5] ); netDict.Add( System.Type.GetType("System.String"), [] ); netDict
netDict =
ConcurrentDictionary<System*Type,System*Double[]> with properties:
Comparer: [1×1 System.Collections.Generic.GenericEqualityComparer<System*Type>]
Count: 3
Keys: [1×1 System.Collections.Generic.SortedDictionary*KeyCollection<System*Type,System*Double[]>]
Values: [1×1 System.Collections.Generic.SortedDictionary*ValueCollection<System*Type,System*Double[]>].NET SortedDictionary を MATLAB ディクショナリに変換します。ここで、System.Type は System.Type にマッピングされ、System.Double[] は System.Double[] にマッピングされます。
d = dictionary(netDict)
d =
dictionary (System.Type ⟼ System.Double[]) with 3 entries:
System.Type ⟼ System.Double[]
System.Type ⟼ System.Double[]
System.Type ⟼ System.Double[]System.Double を System.Object にマッピングするジェネリックの .NET ディクショナリからの MATLAB ディクショナリの作成
System.Double を System.Object にマッピングする .NET Dictionary をインスタンス化します。
netDict = NET.createGeneric( "System.Collections.Generic.Dictionary", ... {"System.Double", "System.Object"} );
3 つの要素を .NET Dictionary に追加します。
netDict.Add(1, 1); netDict.Add(2, [1 2 3 4]); netDict.Add(3, System.DateTime.Now); netDict
netDict =
Dictionary<System*Double,System*Object> with properties:
Comparer: [1×1 System.Collections.Generic.GenericEqualityComparer<System*Double>]
Count: 3
Keys: [1×1 System.Collections.Generic.Dictionary*KeyCollection<System*Double,System*Object>]
Values: [1×1 System.Collections.Generic.Dictionary*ValueCollection<System*Double,System*Object>]
.NET Dictionary を MATLAB ディクショナリに変換します。ここで、System.Double は double にマッピングされ、System.Object は cell にマッピングされます。
d = dictionary(netDict)
d =
dictionary (double ⟼ cell) with 3 entries:
1 ⟼ {[1]}
2 ⟼ {1×1 System.Double[]}
3 ⟼ {1×1 System.DateTime}MATLAB による System.__ComObject の処理方法
System.__ComObject 型は Microsoft® COM オブジェクトを表します。これはパブリック メソッドをもたない mscorlib アセンブリの不可視のパブリック クラスです。特定状況下において、.NET オブジェクトは System.__ComObject のインスタンスを返します。MATLAB は、メタデータで定義されている戻り値の型に基づいて System.__ComObject を処理します。
MATLAB によるオブジェクトの変換
メソッドまたはプロパティの戻り値型が厳密に型指定されていて、呼び出しの結果が System.__ComObject の場合、MATLAB は返されたオブジェクトを適切な型に自動的に変換します。
たとえば、アセンブリが型 TestType を定義し、次のシグネチャをもつメソッド GetTestType を提供するものとします。
| 戻り値の型 | 名前 | 引数 |
|---|---|---|
NetDocTest.TestType RetVal | GetTestType | (NetDocTest.MyClass this) |
GetTestType の戻り値型が厳密に型指定されており、.NET は System.__ComObject 型のオブジェクトを返します。MATLAB は以下の "疑似コード" で示すように、オブジェクトを適切な型の NetDocTest.TestType に自動的に変換します。
cls = NetDocTest.MyClass; var = GetTestType(cls)
var = TestType handle with no properties.
オブジェクトの適切な型へのキャスティング
メソッドまたはプロパティの戻り値型が System.Object で、呼び出しの結果が System.__ComObject の場合、MATLAB は System.__ComObject を返します。返されたオブジェクトを使用するには、有効なクラスまたはインターフェイスの型にキャストします。このオブジェクトの有効なタイプを確認するには、製品ドキュメンテーションを参照してください。
新しいタイプのメンバーを呼び出すには、MATLAB 変換構文を使用してオブジェクトをキャストします。
objConverted = namespace.classname(obj)
ここで、obj は System.__ComObject 型です。
たとえば、Microsoft Excel® シート コレクションの項目は、チャートまたはワークシートにすることができます。以下のコマンドは、System.__ComObject 変数の mySheet を Chart または Worksheet オブジェクトの newSheet に変換します。
newSheet = Microsoft.Office.Interop.Excel.interfacename(mySheet);
ここで、interfacename は、Chart または Worksheet です。例は.NET を使用した Microsoft Excel スプレッドシートでの作業を参照してください。
処理間での COM オブジェクトの受け渡し
関数との間で COM オブジェクトを受け渡しする場合は、オブジェクトがスコープ外になったときに MATLAB によって自動的に解放されないよう、オブジェクトをロックします。オブジェクトをロックするには、関数 NET.disableAutoRelease を呼び出します。これを使用して処理を行った後、関数 NET.enableAutoRelease を使用してオブジェクトのロックを解除します。
MATLAB による System.Nullable の処理方法
.NET が System.Nullable 型を返すと、MATLAB は対応する System.Nullable 型を返します。
System.Nullable 型を使用すると、数値型のような null 値をサポートしていない型に null 値を代入できます。System.Nullable オブジェクトを MATLAB で使用するには、まず null 値の処理方法を決定します。
null値を<ValueType>値と異なる方法で処理する場合は、HasValueプロパティを使用します。すべての値を基になる
<ValueType>にする場合は、GetValueOrDefaultメソッドを使用します。このメソッドでは、<ValueType>型の既定の値をnull値に代入します。
オブジェクトの基となる、MATLAB 式に適した型の変数を使用します。たとえば、System.Nullable 引数の受け渡しを参照してください。
MATLAB による動的な型の処理方法
MATLAB は動的な型を System.Object として処理します。たとえば、以下の C# メソッド exampleMethod は動的な入力引数 d をもち、動的な出力値を返します。
public dynamic exampleMethod(dynamic d)
以下の表は、対応する MATLAB 関数シグネチャを示しています。
| 戻り値の型 | 名前 | 引数 |
|---|---|---|
System.Object RetVal | exampleMethod | ( |
MATLAB でのジャグ配列の処理
.NET ジャグ配列は、MATLAB コマンドで使用する前に変換しておかなければなりません。次のように変換してください。
配列の形状が四角形の場合、対応する MATLAB の数値関数を使用します。
配列の形状が四角形ではない場合、関数
cellを使用します。
ジャグ配列が多次元の場合は、配列を個々の次元ごとに変換しなければなりません。