MATLAB Matlab function errors while using loop

AI Thread Summary
The discussion addresses an error encountered when defining a MATLAB function directly in the command line. The error message indicates that function definitions are not allowed in that context. To resolve this issue, users are advised to create functions in separate .m files. The proper method involves navigating to File -> New -> Function to generate a function template or File -> New -> Script for a blank script. It is recommended to name the .m file the same as the function to ensure it can be called correctly from the command line or other scripts. This approach allows for proper function execution in MATLAB.
adeeyo
Messages
20
Reaction score
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
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.
 

Similar threads

Back
Top