Why cant I create a function in MatLab student version?
4 次查看(过去 30 天)
显示 更早的评论
When I try to create a function i keep getting this error Error: Function definitions are not permitted in this context.
Here is the code I wrote
function sum = myfirst(v,w)
then I get the mentioned error
0 个评论
采纳的回答
Image Analyst
2014-9-6
编辑:Image Analyst
2014-9-6
You forgot to attach your m-file. Most likely, you wrote a script, say test.m, and inside there it starts off with a script and then you include the function below it in the same file, like this
clc;
result = myfirst(10,20);
function theSum = myfirst(v,w)
theSum = v+w;
To fix, declare test a function also so that you have two functions in the file, not a script and a function, like
function test()
clc;
myfirst(10,20);
function theSum = myfirst(v,w)
theSum = v+w;
If this does not fix your problem, then attach your m-file(s).
5 个评论
Image Analyst
2014-9-6
You marked it as accepted, but the comment before last you said "it still won't work" and you didn't attach your m-file like I suggested, so is everything working or not? Again, if you need more help, attach your m-file.
更多回答(1 个)
per isakson
2014-9-6
编辑:per isakson
2014-9-7
Because function sum = myfirst(v,w) must be the first executable line of the m-file. Read Function Basics, Create functions, including anonymous, local, and nested functions
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!