简体   繁体   English

如何从矩阵导入数据文件并从python脚本运行.m文件?

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

I have a .m file that is used to run a neural network in matlab, which I have locally installed on my computer. 我有一个.m文件,用于在matlab中运行神经网络,该文件已本地安装在计算机上。 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. 我正在尝试编写一个python脚本,它将多次遍历神经网络可能的传递和训练功能的列表。 I've written a function to open and edit the .m file, but I don't know how to; 我已经编写了一个打开和编辑.m文件的功能,但是我不知道如何操作。 1. run the .m file from the python script 2. import the necessary data for the neural network as a space delimited matrix. 1.从python脚本运行.m文件。2.将神经网络所需的数据导入为空格分隔的矩阵。

I have three data files that need to be imported as matrices, what would the code look like? 我有三个需要作为矩阵导入的数据文件,代码是什么样的?

Code for the suggestion by Shai could look like this Shai建议代码可能如下所示

#!/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. \\'\\'之间的部分是MATLAB代码。 This could help you to run your MATLAB code with input arguments. 这可以帮助您使用输入参数运行MATLAB代码。 Uncomment logfile = '/dev/null' , if you do not need the output in the logfile. 取消注释logfile = '/dev/null' ,如果您不需要日志文件中的输出。

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

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