How to load .mat file into workspace using Matlab Engine API for python?

36 views (last 30 days)
I have a .mat workspace file containing 4 character variables. These variables contain paths to various folders I need to be able to cd to and from relatively quickly. Usually, when using only Matlab I can load this workspace as follows (provided the .mat file is in the current directory).
load paths.mat
Currently I am experimenting with the Matlab Engine API for python. The Matlab [help docs][1] recommend using the following python formula to send variables to the current workspace in the desktop app:
import matlab.engine
eng = matlab.engine.start_matlab()
x = 4.0
eng.workspace['y'] = x
a = eng.eval('sqrt(y)')
Which works well. However the whole point of the .mat file is that it can quickly load entire *sets*** of variables the user is comfortable with. So the above is not efficient when trying to load the workspace.
I have also tried two different variations in Python:
eng.eval("load paths.mat")
The first variation successfully loads a dict variable in python containing all four keys and values but this does not propagate to the workspace in Matlab. The second variation throws an error:
> File "<string>", line unknown SyntaxError: Error: Unexpected MATLAB > expression.
How do I load up a workspace through the engine without having to manually do it in Matlab? This is an important part of my workflow....

Accepted Answer

J Philps
J Philps on 31 May 2017
There are two things you may want to try:
1. use the function syntax (not command syntax) for load:
Note: you may need to escape the ' characters above, like so:
2. I would try using single quotes:
There is a distinction between "string objects" and 'character arrays' in MATLAB, so that error message about strings may be related to this.

More Answers (0)


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by