执行 .m 使用 oct2py 并存储在变量中(无需将 .mat 存储在磁盘上)

2024-09-27 22:32:08 发布

您现在位置:Python中文网/ 问答频道 /正文

从几个在线想法(即here)来看,我有这样的想法:

  • oct2py(来自Octave)运行一个.m文件
  • 我将其存储在.mat文件中
  • 我用scipy导入这个.mat文件

但是,这种方法的问题是占用了太多的时间和太多的磁盘空间(每个.mat都有5Mb)。我想跑上千遍)。你知道吗

所以我想:

  • oct2py(从八度)运行.m文件
  • 自动保存到python变量,scipy将读取该变量并进行适当的转换。你知道吗

这是可能的和正确的方法吗?你知道吗

我现在正在使用的代码:

from oct2py import octave as oct

currentdirpath = os.getcwd() # get current directory path
currentfoldername = os.path.basename(currentdirpath) # get current folder name

newfoldername = "autogenerated_mats_" + datetime.datetime.now().strftime("%m_%d_%y%H_%M_%S") #creates the name unique folder that will store all the .mats generated;

os.makedirs(newfoldername) #creates an unique folder that will store all the .mats generated; 
numoftimestoexecute = 2 #How many time would you like to execute

for x in range(numoftimestoexecute):

    dateforname = "autogenerated_m_" + datetime.datetime.now().strftime("%m_%d_%y%H_%M_%S")
    saveworkspace = "save -v7 " + currentdirpath + "\\" + newfoldername + "\\" + dateforname + "_workspace.mat"
    saveworkspace = saveworkspace.replace('\\', '/')

    print("Generating " + str(x+1) + " of " + str(numoftimestoexecute))
    oct.eval("Generate_Data")
    #oct.eval("save -v7 myworkspace.mat")
    oct.eval(saveworkspace)

伪代码思想:

storegeneration = oct.eval("Generate_Data")
storemyworkspace = oct.eval("save -v7 myworkspace.mat")

然后,和西皮一起

from scipy.io import loadmat:
D = loadmat(storemyworkspace )

Tags: 文件thedatetimeosevalscipyfolderoct

热门问题