Matlab function errors while using loop

In summary, the error is caused by trying to define a function in the command line instead of saving it in a .m file. To create a new function, go to File -> New -> Function or Script and save it with the same name as the function. This will allow you to call the function from the command line or other scripts/functions.
  • #1
adeeyo
20
0
Hi,

Please assist,

I write this MATLAB function and receive the error below

function f=myfunn(z,x,K,L1,L2,phi)
for c=1:3
f=z(c)-(x(2,c).*K(c).*L1)-(x(1,c).*phi(2,c).*L2)./phi(1,c);
end

function f=myfunn(z,x,K,L1,L2,phi)
|
Error: Function definitions are not permitted in this context.

Please assist me on how to go about it.

Thanks
adeeyo
 
Physics news on Phys.org
  • #2
To define your own functions you must save them in .m files, not just type them into the command line.

To create a new function, go to File -> New -> Function and you will get a shell for a new function. If you want to start completely from scratch, you can go to File -> New -> Script.

Once you've done that, write your function and save it (the praxis is to name the .m file the same as the function inside, i.e. yours would be myfunn.m) and you will be able to call it from the command line (or scripts, or other functions) by calling it as you would any other function in MATLAB.
 

What is a Matlab function error?

A Matlab function error occurs when there is a mistake or issue with the syntax or logic of a function in Matlab. This can prevent the function from running correctly or producing the desired results.

Why do I get Matlab function errors while using loops?

Using loops in Matlab can be tricky and can lead to errors if the syntax or logic of the loop is not properly constructed. Common mistakes include incorrect use of loop variables, incorrect loop conditions, and not properly defining variables within the loop.

How can I fix Matlab function errors while using loops?

To fix Matlab function errors while using loops, carefully check the syntax and logic of your loop. Make sure loop variables are properly defined and used, loop conditions are correctly specified, and all variables are properly initialized within the loop.

Why do I keep getting "Undefined function" errors in my Matlab loop?

An "Undefined function" error in a Matlab loop typically means that the function being called within the loop is not properly defined or does not exist. Check the spelling and syntax of the function being called, and make sure it is properly defined before calling it in the loop.

Is there a way to debug Matlab function errors while using loops?

Yes, Matlab has a built-in debugging tool that can help identify and fix function errors while using loops. This tool allows you to step through your code line by line and see the values of variables, which can help pinpoint where the error is occurring. Additionally, using the "try-catch" statement can help catch errors and provide more detailed information about where the error is occurring.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • Calculus and Beyond Homework Help
Replies
16
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
2K
Back
Top