问题描述
我有一个matlab脚本,我们称其为"master.m",该脚本加载了一个名为"config.m"的文件. config.m包含master.m中使用的所有变量,因此无需编辑任何代码即可轻松更改它们.问题是,我正在尝试使master.m中的主要功能根据用户输入加载config.m.因此,基本上,我希望用户能够指定要加载的文件的名称.例如,如果config.m被称为testing.m,则用户可以在matlab提示符下键入:
I have a matlab script, lets call it "master.m", that loads a file called "config.m". config.m contains all the variables used in master.m so that they can be changed easily without editing any of the code. Problem is, I am trying to get the main function in master.m to load config.m based on the user input. So basically I want the user to be able to specify the name of the file to load. For instance if config.m was called testing.m then the user could type at the matlab prompt:
>> master(testing.m)
,它将加载文件.但是我不知道如何正确执行此操作,我已经研究了eval
函数,但是它给了我一个错误.这是我到目前为止在master.m中拥有的代码:
and it would load up the file. BUT I can't figure out how to do this properly, I have looked into the eval
function but it gives me an error. Here is the code I have as of now in master.m:
function [X,Y] = master(file)
eval(file)
但是,当我在matlab提示符下运行时:
However when I run at the matlab prompt:
>> master(config.m)
??? Attempt to execute SCRIPT config as a function:
/home/myusername/config.m
我也尝试过master('config.m')
,master('./config.m')
,master(config)
和master(config.m)
,但无济于事
I have also tried master('config.m')
,master('./config.m')
, master(config)
and master(config.m)
but to no avail
有什么想法吗?
推荐答案
我认为您想要的是:
master('config');
这篇关于Matlab尝试将脚本作为函数执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!