.NET オブジェクトから返されるデータの処理
MATLAB® から .NET のメソッドまたは関数を呼び出すと、MATLAB は返された .NET スカラー データを自動的に MATLAB 型へ変換します。.NET 関数が配列データを返す場合は、そのデータを明示的に MATLAB 型へ変換できます。
代わりに MATLAB データを .NET メソッドに渡す方法については、.NET オブジェクトへのデータの受け渡しを参照してください。
.NET スカラー データの MATLAB への返却
.NET 関数がスカラー データを返す場合、MATLAB は .NET オブジェクトを次の MATLAB 型へ自動的に変換します。この表では、MATLAB が C# .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.Collections.Generic.IDictionary<,> | System.Collections.Generic.IDictionary<,>.NET ディクショナリの MATLAB ディクショナリへの変換を参照してください。 |
System.Nullable<ValueType> | System.Nullable<ValueType>System.Nullable 引数の受け渡しを参照してください。 |
System.__ComObject | MATLAB が System.__ComObject を処理する方法を参照してください。 |
class name | class name |
struct name | struct name |
dynamic 型 | System.Object |
プリミティブ .NET 型の配列を MATLAB 型に変換
.NET 関数が配列データを返す場合は、次の MATLAB 関数を呼び出して、.NET 配列を等価な MATLAB 配列へ明示的に変換できます。
System.String から MATLAB の string または文字ベクトルへの変換
.NET 関数が System.String オブジェクトを返す場合、MATLAB はそのデータを MATLAB 型へ自動的に変換しません。ただし、MATLAB には、標準オブジェクトが表示される代わりに、System.String オブジェクトの文字列の値が表示されます。以下に例を示します。
a = System.String("test") b = System.String.Concat(a," hello"," world")
a = test b = test hello world
System.String オブジェクトを MATLAB string に変換するには、string 関数を使用します。System.String オブジェクトを MATLAB 文字ベクトルに変換するには、char 関数を使用します。以下に例を示します。
str = System.String("My System.String");
mlstr = string(str)
mlchar = char(str)
mlstr =
"My System.String"
mlchar =
'My System.String'
string 関数は、String.String 配列 (String.String[] や String.String[,] など) を、同じ次元とサイズの MATLAB string 配列に変換します。String.String[][] のようなジャグ配列の変換はサポートされていません。
MATLAB が System.Decimal を処理する方法
MATLAB で System.Decimal 型を使用するには、System.Decimal.ToDouble 関数を呼び出します。System.Decimal 変数を作成します。
xdec = System.Decimal(100.1)
xdec =
Decimal with properties:
Scale: 1
Zero: [1×1 System.Decimal]
One: [1×1 System.Decimal]
MinusOne: [1×1 System.Decimal]
MaxValue: [1×1 System.Decimal]
MinValue: [1×1 System.Decimal]MATLAB double に変換するには、次のようにします。
xdouble = System.Decimal.ToDouble(xdec)
xdouble = 100.1000
MATLAB が System.Byte を処理する方法
イメージを System.Byte 配列として返す C# ライブラリ関数があるとします。MATLAB でそのデータを使用するには、How to convert raw byte data from a C# library to an image? を参照してください。
.NET ディクショナリの MATLAB ディクショナリへの変換
オブジェクトの関数 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 が dynamic 型を処理する方法
MATLAB は動的な型を System.Object として処理します。たとえば、次の C# メソッド exampleMethod は動的な入力引数 d をもち、動的な出力値を返します。
public dynamic exampleMethod(dynamic d)
次の表は、対応する MATLAB 関数シグネチャを示しています。
| 戻り値の型 | 名前 | 引数 |
|---|---|---|
System.Object RetVal | exampleMethod | ( |
MATLAB でのジャグ配列の処理
.NET ジャグ配列は、MATLAB コマンドで使用する前に変換しておかなければなりません。次のように変換してください。
配列の形状が矩形の場合、対応する MATLAB の数値関数を使用します。
配列の形状が矩形ではない場合、関数
cellを使用します。
ジャグ配列が多次元の場合は、配列を個々の次元ごとに変換しなければなりません。