python 的 matlab 引擎中的 save() 命令
save() command in matlab engine for python
我正在为 Python 使用 MATLAB 引擎 API
https://nl.mathworks.com/help/matlab/matlab-engine-for-python.html
我想打开并保存一个文件。
#import and start the engine
import matlab.engine
eng = matlab.engine.start_matlab()
print('Matlab engine started')
#File of interest
myBadFile='test.mat'
#Synchronize python/matlab working directory
eng.cd(os.getcwd(),nargout=0)
print(eng.pwd())
#Read file contents
VALUES=eng.load(myBadFile,nargout=1)
到目前为止一切顺利。我真的很惊讶它运行得如此顺利。
我在VALUES
上做我的东西,然后我想再次保存它。
如果我这样做
VALUES=eng.save(myBadFile+'.test','VALUES','-v6',nargout=0)
我得到:
MatlabExecutionError: Variable 'VALUES' not found.
如果我这样做
VALUES=eng.save(myBadFile+'.test',VALUES,'-v6',nargout=0)
我明白了
MatlabExecutionError: Argument must contain a character vector.
那么如何保存我的 VALUES,它在 python 环境中是一个有效变量,但在 matlab 中显然看不到它?
save
operates on the variables contained in MATLAB's workspace, and Python does not share scope with the MATLAB engine instance(s). The matlab.engine
instance, however, does have a workspace
属性,定义如下:
Python dictionary containing references to MATLAB variables. You can
assign data to, and get data from, a MATLAB variable through the
workspace
. The name of each MATLAB variable you create becomes a key
in the workspace
dictionary. The keys in workspace
must be valid
MATLAB identifiers (for example, you cannot use numbers as keys).
您可以使用它在 MATLAB 的范围内放置变量。
此代码,例如:
import matlab.engine
eng = matlab.engine.start_matlab()
x = [1, 2, 3]
eng.save('test.mat', 'x')
如上失败:
Error using save
Variable 'x' not found.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\excaza\AppData\Roaming\Python\Python36\site-packages\matlab\engine\matlabengine.py", line 78, in __call__
_stderr, feval=True).result()
File "C:\Users\excaza\AppData\Roaming\Python\Python36\site-packages\matlab\engine\futureresult.py", line 68, in result
return self.__future.result(timeout)
File "C:\Users\excaza\AppData\Roaming\Python\Python36\site-packages\matlab\engine\fevalfuture.py", line 82, in result
self._result = pythonengine.getFEvalResult(self._future,self._nargout, None, out=self._out, err=self._err)
matlab.engine.MatlabExecutionError: Variable 'x' not found.
但是一旦我们将 x
复制到 workspace
字典中就可以正常工作:
import matlab.engine
eng = matlab.engine.start_matlab()
x = [1, 2, 3]
eng.workspace['x'] = x
eng.save('test.mat', 'x')
我正在为 Python 使用 MATLAB 引擎 API https://nl.mathworks.com/help/matlab/matlab-engine-for-python.html
我想打开并保存一个文件。
#import and start the engine
import matlab.engine
eng = matlab.engine.start_matlab()
print('Matlab engine started')
#File of interest
myBadFile='test.mat'
#Synchronize python/matlab working directory
eng.cd(os.getcwd(),nargout=0)
print(eng.pwd())
#Read file contents
VALUES=eng.load(myBadFile,nargout=1)
到目前为止一切顺利。我真的很惊讶它运行得如此顺利。
我在VALUES
上做我的东西,然后我想再次保存它。
如果我这样做
VALUES=eng.save(myBadFile+'.test','VALUES','-v6',nargout=0)
我得到:
MatlabExecutionError: Variable 'VALUES' not found.
如果我这样做
VALUES=eng.save(myBadFile+'.test',VALUES,'-v6',nargout=0)
我明白了
MatlabExecutionError: Argument must contain a character vector.
那么如何保存我的 VALUES,它在 python 环境中是一个有效变量,但在 matlab 中显然看不到它?
save
operates on the variables contained in MATLAB's workspace, and Python does not share scope with the MATLAB engine instance(s). The matlab.engine
instance, however, does have a workspace
属性,定义如下:
Python dictionary containing references to MATLAB variables. You can assign data to, and get data from, a MATLAB variable through the
workspace
. The name of each MATLAB variable you create becomes a key in theworkspace
dictionary. The keys inworkspace
must be valid MATLAB identifiers (for example, you cannot use numbers as keys).
您可以使用它在 MATLAB 的范围内放置变量。
此代码,例如:
import matlab.engine
eng = matlab.engine.start_matlab()
x = [1, 2, 3]
eng.save('test.mat', 'x')
如上失败:
Error using save
Variable 'x' not found.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\excaza\AppData\Roaming\Python\Python36\site-packages\matlab\engine\matlabengine.py", line 78, in __call__
_stderr, feval=True).result()
File "C:\Users\excaza\AppData\Roaming\Python\Python36\site-packages\matlab\engine\futureresult.py", line 68, in result
return self.__future.result(timeout)
File "C:\Users\excaza\AppData\Roaming\Python\Python36\site-packages\matlab\engine\fevalfuture.py", line 82, in result
self._result = pythonengine.getFEvalResult(self._future,self._nargout, None, out=self._out, err=self._err)
matlab.engine.MatlabExecutionError: Variable 'x' not found.
但是一旦我们将 x
复制到 workspace
字典中就可以正常工作:
import matlab.engine
eng = matlab.engine.start_matlab()
x = [1, 2, 3]
eng.workspace['x'] = x
eng.save('test.mat', 'x')