Main Content

Use Python Dictionaries in MATLAB

You can convert a Python® dictionary (dict) to a MATLAB dictionary or structure, or vice versa.

  • You can convert a Python dictionary to a MATLAB dictionary using the dictionary function. Alternatively, you can convert a Python dictionary to a MATLAB structure using the struct function.

  • You can convert a MATLAB dictionary or structure to a Python dictionary using the py.dict function. You can also pass MATLAB dictionaries directly to Python functions without converting them to Python dictionaries first.

Convert Python Dictionary to MATLAB Dictionary or Structure

If you have a Python dictionary, you can convert it to a MATLAB dictionary using the dictionary function. (since R2024a)

For example, create a Python dictionary and convert it to a MATLAB dictionary. In this case, MATLAB converts the keys to strings and the values to doubles.

dp = py.dict(soup=3.57,bread=2.29,bacon=3.91,salad=5.00);  
dm = dictionary(dp) 
dm =

  dictionary (string ⟼ double) with 4 entries:

    "soup"  ⟼ 3.5700
    "bread" ⟼ 2.2900
    "bacon" ⟼ 3.9100
    "salad" ⟼ 5

MATLAB converts the Python keys and values to the equivalent type in MATLAB when possible. If the Python dictionary keys are not all of the same data type when automatically converted to a MATLAB data type, the keys are wrapped in cell arrays. The same is true of the Python dictionary values.

Alternatively, you can convert Python dictionaries to MATLAB structures using the struct function as long as the Python dictionary keys are valid MATLAB identifiers.

sm = struct(dp)
sm = struct with fields:
     soup: 3.5700
    bread: 2.2900
    bacon: 3.9100
    salad: 5

Convert MATLAB Dictionary to Python Dictionary

MATLAB implicitly converts any MATLAB dictionary passed to a Python function into a Python dictionary. However, if you have a MATLAB dictionary, you can alternatively convert it explicitly to a Python dictionary using py.dict. The keys and values of the Python dictionary are determined by the default conversion rules for MATLAB types, which are described at Pass MATLAB Data to Python.

For example, create a MATLAB dictionary and pass it directly to the Python function py.len without converting it to a Python dictionary first.

dm = dictionary(["Robert" "Mary" "Jack"],[357 229 391]);   
l = py.len(dm)
l = 
  Python int with properties:

    denominator: [1×1 py.int]
           imag: [1×1 py.int]
      numerator: [1×1 py.int]
           real: [1×1 py.int]

    3

You can also convert the MATLAB dictionary to a Python dictionary using py.dict.

dp = py.dict(dm)
dp = 
  Python dict with no properties.

    {'Robert': 357.0, 'Mary': 229.0, 'Jack': 391.0}

Use Python Dictionary Methods in MATLAB

You can use a Python dictionary method in MATLAB by calling the method as a function and passing the Python dictionary as its first argument.

For example, change the keys and values in a Python dictionary using the update method. First, create a Python dictionary of patients and test results. Then, update the results for test1.

patientp = py.dict(name="John Doe", ...
    test1 = [], ...
    test2 = [220.0, 210.0, 205.0], ...
    test3 = [180.0, 178.0, 177.5]);

new_test1 = py.dict(test1=[79.0, 75.0, 73.0]);

% This call is equivalent to the Python command patient.update(new_test1)
update(patientp,new_test1);

You can use a Python dictionary and a MATLAB dictionary together to display output.

patientm = dictionary(patientp);

x = sprintf("%s's test1 results: %d %d %d", ...
    string(patientp{"name"}), double(patientm{("test1")}(1)), ...
    double(patientm{("test1")}(2)), double(patientm{("test1")}(3)));
disp(x)
John Doe's test1 results: 79 75 73

See Also

|

Related Topics