使用参数运行Matlab程序 [英] Running a matlab program with arguments

查看:283
本文介绍了使用参数运行Matlab程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个matlab文件,其中包含一个文件.我想在matlab shell中运行该程序,例如prog.我需要实现它,以便它采用许多参数,例如"prog filename.txt 1 2,这意味着我可以在程序中使用filename.txt和1 2作为变量.

I have a matlab file that takes in a file. I would like to run that program in the matlab shell, such as prog. I need to implement it so that it takes a number of arguments, such as "prog filename.txt 1 2 which would mean that i can use filename.txt and 1 2 as variables in my program.

谢谢!

推荐答案

为了使脚本从命令行接受参数,必须首先将其转换为将获取所需参数的函数,即,如果脚本被命名为prog.m,放在第一行

In order to make a script accept arguments from the command line, you must first turn it into a function that will get the arguments you want, i.e if your script is named prog.m, put as the first line

function []=prog(arg1, arg2)

,并在末尾添加end(假设文件只有一个功能). 非常非常重要,您要将该函数命名为与文件相同的名称.

and add an end at the end (assuming that the file has only one function). It's very important that you call the function the same name as the file.

接下来的事情是,您需要确保脚本文件与调用脚本的位置相同,或者位于Matlab的工作路径中,否则它将无法识别您的脚本

The next thing is that you need to make sure that the script file is located at the same place from where you call the script, or it's located at the Matlab working path, otherwise it'll not be able to recognize your script.

最后,要执行脚本,请使用

Finally, to execute the script you use

matlab -r "prog arg1 arg2"

相当于调用

prog(arg1,arg2)

从Matlab内部.

*-在Windows和Linux环境中进行了测试

*- tested in Windows and Linux environments

这篇关于使用参数运行Matlab程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆