matlab attempt to execute,Matlab Attempt to execute script as a function

问题

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)

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)

However when I run at the matlab prompt:

>> master(config.m)

??? Attempt to execute SCRIPT config as a function:

/home/myusername/config.m

I have also tried master('config.m'),

master('./config.m'), master(config) and master(config.m) but to no avail

Any ideas?

回答1:

I think what you want is:

master('config');

回答2:

You almost had it! Enclosing the filename in single quotes and dropping .m should do the trick. The following works for me on Matlab R2011b (Linux) with the files master.m and config.m in the same folder.

master.m:

function X = master( filename )

eval(filename);

X = a^2;

end

config.m:

a = 2;

disp('Testing config.m');

In console:

>> master('config')

Testing config.m

ans =

4

来源:https://stackoverflow.com/questions/9826826/matlab-attempt-to-execute-script-as-a-function

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值