Why cant I create a function in MatLab student version?

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

 채택된 답변

Image Analyst
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

Also note I used "theSum" instead of sum(). You never, or almost never, want to use the name of a built-in function, particularly one as important as sum(), as the name of one of your variables.
Ive tried everything and it still won't work. Its not something silly like you can create a function in the student version?
No, there is no issue with the student version. My guess is you are just not understanding the difference between m-files in general, functions and scripts. Spend some time reading about them.
I got it, I am very new to this and my homework tutorial is pretty bland. I understand now about the m files and you have to create an m file and so on. Thanks for the help! Much appreciated!
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
per isakson 2014년 9월 6일
편집: per isakson 2014년 9월 7일

0 개 추천

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

카테고리

도움말 센터File Exchange에서 Adding custom doc에 대해 자세히 알아보기

질문:

2014년 9월 6일

편집:

2014년 9월 7일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by