繁体   English   中英

将 matlab.object 转换为 pandas dataframe

[英]Converting matlab.object to a pandas dataframe

I have used this code to run a matlab script inside python, the script is the file'matlab_to_python.m' and it has 1 output: a matlab table called 'matlab_table'.

import matlab.engine
eng = matlab.engine.start_matlab()

mytable = eng.matlab_to_python()

当我运行它时,mytable 的类型是:

In [28]: mytable
Out[28]: <matlab.object at 0x1ebe007fb70>

我希望能够在 python 中将此表视为 pandas dataframe,知道如何完成吗?

谢谢!

尽管似乎尚不支持将表从 MATLAB 直接传输到 Python 中,但您可以利用字符转换来弥补差异。

For example, you might convert the table into json format (1xN char), which could then be turned into a DataFrame using the pandas.DataFrame.read_json function.

import matlab.engine
import pandas as pd
eng = matlab.engine.start_matlab()

# keep the table in MATLAB
eng.workspace['mytable'] = eng.matlab_to_python()

# pass a string representation to Python
jsontable = eng.eval('jsonencode(mytable)')

# convert back into a table
mytable = pd.read(jsontable)

An alternate approach could be to write from matlab to a csv file, which you can then read into Python via pandas.DataFrame.read_csv

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM