メインコンテンツ

.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.Int16int16 スカラー
System.UInt16uint16 スカラー
System.Int32int32 スカラー
System.UInt32uint32 スカラー
System.Int64int64 スカラー
System.UInt64uint64 スカラー
System.Singlesingle スカラー
System.Doubledouble スカラー
System.Booleanlogical スカラー
System.Byteuint8 スカラー
System.Enumenum
System.Charchar
System.DecimalSystem.Decimal
System.ObjectSystem.Object
System.IntPtrSystem.IntPtr
System.UIntPtrSystem.UIntPtr
System.StringSystem.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 nameclass name
struct namestruct name
dynamicSystem.Object

プリミティブ .NET 型の配列を MATLAB 型に変換

.NET 関数が配列データを返す場合は、次の MATLAB 関数を呼び出して、.NET 配列を等価な MATLAB 配列へ明示的に変換できます。

C# .NET 型MATLAB の変換関数
System.Int16

int16

System.UInt16uint16
System.Int32int32
System.UInt32uint32
System.Int64int64
System.UInt64uint64
System.Singlesingle
System.Doubledouble
System.Booleanlogical
System.Byteuint8
System.Array

.NET アプリケーションによる配列の使用を参照してください。

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[][] のようなジャグ配列の変換はサポートされていません。

MATLABSystem.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

MATLABSystem.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.Int32System.Double にマッピングするジェネリックの .NET ディクショナリからの MATLAB ディクショナリの作成

System.Int32System.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.Int32int32 にマッピングされ、System.Doubledouble にマッピングされます。

d = dictionary(netDict)
d =
  dictionary (int32 ⟼ double) with 3 entries:
    1 ⟼ 1.1
    2 ⟼ 2.2
    3 ⟼ 3.3

System.CharSystem.String にマッピングするジェネリックの .NET ConcurrentDictionary からの MATLAB ディクショナリの作成

System.CharSystem.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.Charchar (ディクショナリで string に変換される) にマッピングされ、System.StringSystem.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.TypeSystem.Double[] にマッピングするジェネリックの .NET SortedDictionary からの MATLAB ディクショナリの作成

System.TypeSystem.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.TypeSystem.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.DoubleSystem.Object にマッピングするジェネリックの .NET ディクショナリからの MATLAB ディクショナリの作成

System.DoubleSystem.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}

MATLABSystem.__ComObject を処理する方法

System.__ComObject 型は Microsoft® COM オブジェクトを表します。これはパブリック メソッドをもたない mscorlib アセンブリの不可視のパブリック クラスです。特定状況下において、.NET オブジェクトは System.__ComObject のインスタンスを返します。MATLAB は、メタデータで定義されている戻り値の型に基づいて System.__ComObject を処理します。

MATLAB によるオブジェクトの変換

メソッドまたはプロパティの戻り値型が厳密に型指定されていて、呼び出しの結果が System.__ComObject の場合、MATLAB は返されたオブジェクトを適切な型に自動的に変換します。

たとえば、アセンブリが TestType 型を定義し、次のシグネチャをもつメソッド GetTestType を提供するものとします。

戻り値の型名前引数
NetDocTest.TestType RetValGetTestType(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)

ここで、objSystem.__ComObject 型です。

たとえば、Microsoft Excel® シート コレクションの項目は、チャートまたはワークシートにすることができます。次のコマンドは、System.__ComObject 変数 mySheetChart または Worksheet オブジェクト newSheet に変換します。

newSheet = Microsoft.Office.Interop.Excel.interfacename(mySheet);

ここで、interfacename は、Chart または Worksheet です。例は.NET を使用した Microsoft Excel スプレッドシートでの作業を参照してください。

処理間での COM オブジェクトの受け渡し

関数との間で COM オブジェクトを受け渡しする場合は、オブジェクトがスコープ外になったときに MATLAB によって自動的に解放されないよう、オブジェクトをロックします。オブジェクトをロックするには、関数 NET.disableAutoRelease を呼び出します。これを使用して処理を行った後、関数 NET.enableAutoRelease を使用してオブジェクトのロックを解除します。

MATLABSystem.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 引数の受け渡しを参照してください。

MATLABdynamic 型を処理する方法

MATLAB は動的な型を System.Object として処理します。たとえば、次の C# メソッド exampleMethod は動的な入力引数 d をもち、動的な出力値を返します。

public dynamic exampleMethod(dynamic d)

次の表は、対応する MATLAB 関数シグネチャを示しています。

戻り値の型名前引数
System.Object RetValexampleMethod(namespace.classname this,
System.Object d)

MATLAB でのジャグ配列の処理

.NET ジャグ配列は、MATLAB コマンドで使用する前に変換しておかなければなりません。次のように変換してください。

  • 配列の形状が矩形の場合、対応する MATLAB の数値関数を使用します。

  • 配列の形状が矩形ではない場合、関数 cell を使用します。

ジャグ配列が多次元の場合は、配列を個々の次元ごとに変換しなければなりません。

参考

トピック