How do I import a data file as a matrix and run a .m file from a python script?

早过忘川 提交于 2019-12-20 04:22:08

问题


I have a .m file that is used to run a neural network in matlab, which I have locally installed on my computer. I am trying to write a python script that will loop through a list of possible transfer and training functions for the neural network multiple times. I've written a function to open and edit the .m file, but I don't know how to; 1. run the .m file from the python script 2. import the necessary data for the neural network as a space delimited matrix.

I have three data files that need to be imported as matrices, what would the code look like?


回答1:


Code for the suggestion by Shai could look like this

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os

your_dir   = '/path/to/your/mfile'
your_mfile = 'name_of_mfile_without.m'
logfile    = '/path/to/save/matlab/standard_out.txt'
# logfile  = '/dev/null'

transfer_functions = ['func_1','func_2']

for f in transfer_functions:
    os.system(' matlab -nodesktop -nosplash -nodisplay -r  \' '
              ' addpath ' + your_dir + ' ;                    '
              your_mfile + ' ' +  f  + ' ;                    '
              ' exit                     ;                 \' '
              '  > ' + logfile                                 )

The part between \' and \' is MATLAB code. This could help you to run your MATLAB code with input arguments. Uncomment logfile = '/dev/null', if you do not need the output in the logfile.



来源:https://stackoverflow.com/questions/20769415/how-do-i-import-a-data-file-as-a-matrix-and-run-a-m-file-from-a-python-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!