6

I'm playing with the new capability in MATLAB 2014b where you can call python directly from matlab and get python objects in the workspace (like you've been able to with Java for a long time). I have successfully called a function and gotten a dictionary into the workspace, but I'm stuck on how to harvest the values from it. In this case I have a dictionary full of dictionaries, so I can't just convert it to MATLAB cells like they do in their example.

So a specific question: if I have a dictionary in MATLAB called "A", how to I get the sub-dictionary A['2'] out?

3
  • Please post code that recreates the data you are working with. See MVCE. Commented Jan 23, 2015 at 16:45
  • I can't share the exact code for various reasons, but the MATLAB part is like this: A = py.pythonfile.pythonfunc(arg), and A comes back as a dictionary. If '2' is a key in A, I could use A['2'] in python, but I don't know what the equivalent is in MATLAB. Thanks. Commented Jan 23, 2015 at 17:09
  • 1
    Whether or not it is the exact code does not matter. All that matters is that the code provided reproduces the problem. Commented Jan 23, 2015 at 17:10

1 Answer 1

3

By consulting the MATLAB External Interfaces documentation, the Python dict type is essentially the same interface as a containers.Map. You need to use the values function to extract the value that you want for a certain key. As such, you would call values like so, given that your Python dictionary is stored in A and you want to use '2' as the key to index into your dictionary:

val = values(A, '2');

As such, val would contain what the associated value is with the key of '2'. MATLAB also has the ability to use multiple keys and it would return multiple values - one per key. Therefore, you could also do something like this:

val = cell(values(A, {'1', '2', '3'}));

val would be a three element cell array, where each element is the value for the associated keys that you input in. It is imperative that you convert the output of values into a cell array, because this would normally be a list in Python. In order to use these results in MATLAB, we need to convert to a cell.

Therefore, val{1} would be the dictionary output when you used '1' as the key. Similarly, val{2} would be the dictionary output when you used '2' as the key and so on.


Here are some more operations on a containers.Map object for you. If you want all of the keys in a dictionary, use the keys function:

k = keys(A);

If you were to just use values with the dictionary by itself, like so:

val = cell(values(A));

It would produce all values for every key that exists in your dictionary stored into a cell array.

If you wanted to update a particular key in your Python dictionary, use the update function:

update(A,py.dict(pyargs('4',5.677)));

Here, we use the dictionary A, and update the key-value pair, where the key is '4' and the new value is 5.677.

Sign up to request clarification or add additional context in comments.

4 Comments

And 20k! Congrats bud!
@Divakar thank you my friend :) took a while, but got there eventually!
It's been crazy, almost a year for both of us on here, helluva of a year this was (eventful and so much of learning)!! :)
@Divakar Oh yes! It was a rush most definitely. I've learned more about matlab in the few months that I started posting here than in my entire time in graduate school.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.