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

该博客介绍如何通过Python脚本调用Matlab,以循环方式运行神经网络并应用不同的转移和训练函数。提供了具体的Python代码示例,用于添加路径、打开和编辑.m文件,并将标准输出重定向到日志文件。代码中利用os.system执行Matlab命令,实现了与Matlab的交互。
摘要由CSDN通过智能技术生成

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?

解决方案

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值