How to load .mat file into workspace using Matlab Engine API for python?
22 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Amine Aboufirass
el 26 de Mayo de 2017
Respondida: J Philps
el 31 de Mayo de 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).
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 comentarios
Respuesta aceptada
J Philps
el 31 de Mayo de 2017
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 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Call MATLAB from Python en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!