How to load .mat file into workspace using Matlab Engine API for python?
36 views (last 30 days)
Show older comments
Amine Aboufirass on 26 May 2017
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).
Currently I am experimenting with the Matlab Engine API for python. The Matlab [help docs] recommend using the following python formula to send variables to the current workspace in the desktop app:
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:
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....
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)
Find more on Call MATLAB from Python in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!