Python 如何使用我的参数创建matlab文件?

Python 如何使用我的参数创建matlab文件?,python,Python,我正在编写一个函数,将2d python数组转换为matlab文件。这是我到目前为止的代码 def save_array(arr,fname): import scipy.io import numpy out_dict={} out_dict[fname]=arr scipy.io.savemat(fname.mat,out_dict)` 我希望fname是一个字符串,但我不确定如何让savemat部分工作 import scipy.io import numpy as np

我正在编写一个函数,将2d python数组转换为matlab文件。这是我到目前为止的代码

def save_array(arr,fname):

import scipy.io 
import numpy



out_dict={}

out_dict[fname]=arr

scipy.io.savemat(fname.mat,out_dict)`
我希望fname是一个字符串,但我不确定如何让savemat部分工作

import scipy.io
import numpy as np

def save_array(arr, arrname, fname):
    """
    Save an array to a .mat file
    Inputs:
      arr: ndarray to save
      arrname: name to save the array as (string)
      fname: .mat filename (string)
    """
    out_dict={arrname: arr}
    scipy.io.savemat(fname,out_dict)

save_array(np.array([1,2,3]), 'arr', 'test.mat')

也许值得做一两个python教程。这是非常基本的东西

你看过如何调用savemat的参考文档了吗:fname应该包含.mat。好吧,那是我的问题。谢谢