Main Content

Use MATLAB Dictionary Objects in .NET

These examples show how MATLAB® converts between MATLAB dictionary and .NET dictionary objects.

Convert MATLAB Dictionary to .NET Dictionary

You can convert a MATLAB dictionary to any of these .NET types:

  • System.Collections.Generic.Dictionary<TKey,TValue>

  • System.Collections.Generic.IDictionary<TKey,TValue>

  • System.Collections.Generic.IReadOnlyDictionary<TKey,TValue>

  • System.Collections.IDictionary

For more information, see Pass .NET Dictionary to MATLAB.

Create MATLAB Dictionary and Display in .NET Application

This C# code creates a MATLAB dictionary that maps a string to an integer.

using System;
using System.Collections;
using System.Collections.Generic;
using MathWorks.MATLAB.Engine;
 
// Create a MATLAB dictionary mapping string->int.
using dynamic eng = MATLABEngine.StartMATLAB();
dynamic mlDict = eng.dictionary("a", 1, "b", 2);
 
// Convert to .NET dictionary and display.
Dictionary<string, int> netDict = mlDict;
foreach (var kv in netDict)
  Console.WriteLine("Key: {0}, Value: {1}", kv.Key, kv.Value);

Executing the code displays:

Key: a, Value: 1
Key: b, Value: 2

Convert int32 to string

using System;
using System.Collections;
using System.Collections.Generic;
using MathWorks.MATLAB.Engine;
 
// Start MATLAB engine.
using dynamic eng = MATLABEngine.StartMATLAB();
   
// Create a MATLAB dictionary mapping int32->string.
dynamic mlDict = eng.dictionary(1, "a", 2, "b");
 
// Convert to a .NET dictionary and display.
Dictionary<int, string> netDict = mlDict;
foreach (var kv in netDict)
  Console.WriteLine("Key: {0}, Value: {1}", kv.Key, kv.Value);

Executing the code displays:

Key: 1, Value: a
Key: 2, Value: b

Convert double to matlab.graphics.Graphics

using System;
using System.Collections.Generic;
using MathWorks.MATLAB.Engine;
using MathWorks.MATLAB.Types;
 
// Start MATLAB engine.
using dynamic eng = MATLABEngine.StartMATLAB();
   
// Create two figures.
MATLABObject f1 = eng.figure();
MATLABObject f2 = eng.figure();
 
// Create a MATLAB dictionary mapping int32->figure.
dynamic mlDict = eng.dictionary(1.0, f1, 2.0, f2);
 
// Convert to a .NET dictionary and display.
IDictionary<double, MATLABObject> netDict = mlDict;
foreach (var kv in netDict)
  Console.WriteLine("Key: {0}, Value: {1}", kv.Key, kv.Value);

Executing the code displays:

Key: 1.0, Value: MATLABObject
Key: 2.0, Value: MATLABObject

Convert double to cell

using System;
using System.Collections;
using MathWorks.MATLAB.Engine;
using MathWorks.MATLAB.Types;
 
// Start MATLAB engine.
using (dynamic eng = MATLABEngine.StartMATLAB()) {
   
/* Create two entries to place into the dictionary. */
 
dynamic mlDict = eng.eval("
dictionary(3.14, {[1 2 3]}, 42, {{'this', 'is'; '2x2', 'cell'}})");
 
// Convert to a .NET dictionary and display.
IDictionary<double, object> netDict = mlDict;
foreach (var kv in netDict)
  Console.WriteLine("Key: {0}, Value: {1}", kv.Key, kv.Value);

Executing the code displays:

Key: 3.14, Value: MATLABArray
Key: 42.0, Value: MATLABArray

Handle Dictionary Returned from MATLAB Function in .NET

You can convert a .NET object whose type implements the System.Collections.Generic.IDictionary<TKey,TValue> interface to a MATLAB dictionary. The type of keys (TKey) and values (TValue) must be of the types described in MATLAB Dictionary Type in .NET.

Create .NET dictionary and Display in MATLAB

This C# code creates a .NET dictionary and displays its elements in MATLAB.

// Create a .NET dictionary and add elements.
Dictionary<string, int> netDict = new Dictionary<string, int>();
netDict.Add("a", 1);
netDict.Add("b", 2);
 
// Convert to MATLAB dictionary.
dynamic eng = MATLABEngine.StartMATLAB();
RunOptions opts = new RunOptions(nargout: 0);
eng.disp(opts, netDict);

Executing the code displays:

  dictionary (string ⟼ double) with 2 entries:
 
    "a" ⟼ 1
    "b" ⟼ 2

Convert Int32 to Double

using MathWorks.MATLAB.Engine;
using MathWorks.MATLAB.Types;
using System;
using System.Collections.Generic;
 
// Start MATLAB engine.
using dynamic eng = MATLABEngine.StartMATLAB();
 
// Create a .NET dictionary.
var dict = new Dictionary<int, double>();
dict.Add(1, 1.1);
dict.Add(2, 2.2);
dict.Add(3, 3.3);
 
// Convert to MATLAB dictionary and display.
var opts = new RunOptions(nargout: 0);
eng.disp(opts, dict);

Executing the code displays:

  dictionary (int32 ⟼ double) with 3 entries:
 
    1 ⟼ 1.1000
    2 ⟼ 2.2000
    3 ⟼ 3.3000

Convert Char to MATLABObject

using MathWorks.MATLAB.Engine;
using MathWorks.MATLAB.Types;
using System;
using System.Collections.Concurrent;
 
// Start MATLAB engine.
using dynamic eng = MATLABEngine.StartMATLAB();
   
// Create a .NET dictionary.
var dict = new ConcurrentDictionary<char, MATLABObject>();
 
// Add elements to the dictionary. For keys, use MATLAB figures.
dict.Add('a', eng.figure());
dict.Add('b', eng.figure());   
 
// Convert to MATLAB dictionary and display.
var opts = new RunOptions(nargout: 0);
eng.disp(opts, dict);

Executing the code displays:

  dictionary (string ⟼ matlab.graphics.Graphics) with 3 entries:
 
    "a" ⟼ 1x1 Figure
    "b" ⟼ 1x1 Figure

Convert Int32 to MATLABStruct

using MathWorks.MATLAB.Engine;
using MathWorks.MATLAB.Types;
using System;
using System.Collections.Generic;
 
// Start MATLAB engine
using dynamic eng = MATLABEngine.StartMATLAB();
   
// Create a .NET dictionary.
var dict = new Dictionary<int, MATLABStruct>();
 
// Add two elements to the dictionary.
dict.Add(1, new MATLABStruct( ("a",1), ("b",2) ));
dict.Add(2, new MATLABStruct( ("c",3) ));
 
// Convert to MATLAB dictionary and display.
var opts = new RunOptions(nargout: 0);
eng.disp(opts, dict);

Executing the code displays:

  dictionary (int32 ⟼ struct) with 3 entries:
 
    1 ⟼ 1x1 struct
    2 ⟼ 1x1 struct

Convert Int32 to Object

using MathWorks.MATLAB.Engine;
using MathWorks.MATLAB.Types;
using System;
using System.Collections.Immutable;
 
// Start MATLAB engine.
using dynamic eng = MATLABEngine.StartMATLAB();
   
// Create a .NET dictionary.
var dict = new ImmutableDictionary<int, object>();
 
// Add a value that converts to a 1x3 double vector.
var entry1 = new double[]{ 1, 2, 3 };
dict = dict.Add(1, entry1);
 
// Add a value that converts to a 2x2 cell array.
var entry2 = new object[,] {
  { "this", "is" },
  { "a", "matrix" }
}
dict = dict.Add(2, entry2);
 
// Convert to MATLAB dictionary and display.
var opts = new RunOptions(nargout: 0);
eng.disp(opts, dict);

Executing the code displays:

  dictionary (int32 ⟼ cell) with 2 entries:
 
    1 ⟼ {[1 2 3]}
    2 ⟼ {2×2 cell}

Related Topics