How to load .mat file into workspace using Matlab Engine API for python?
18 次查看(过去 30 天)
显示 更早的评论
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)')
print(a)
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.load("paths.mat")
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....
[1]: https://nl.mathworks.com/help/matlab/matlab_external/use-the-matlab-engine-workspace-in-python.html
0 个评论
采纳的回答
J Philps
2017-5-31
There are two things you may want to try:
1. use the function syntax (not command syntax) for load:
eng.eval("load('paths.mat')")
Note: you may need to escape the ' characters above, like so:
eng.eval("load(\'paths.mat\')")
2. I would try using single quotes:
eng.load('paths.mat')
There is a distinction between "string objects" and 'character arrays' in MATLAB, so that error message about strings may be related to this.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Call MATLAB from Python 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!