Matlab function errors while using loop

Click For Summary
SUMMARY

The discussion addresses an error encountered in MATLAB when defining a function within a loop. The error message "Function definitions are not permitted in this context" indicates that the function must be saved in a separate .m file rather than being typed directly into the command line. To resolve this, users should create a new function by navigating to File -> New -> Function, write their function code, and save it as myfunn.m. This allows the function to be called correctly from the command line or other scripts.

PREREQUISITES
  • Basic understanding of MATLAB programming
  • Knowledge of function definitions in MATLAB
  • Familiarity with .m file structure
  • Experience with MATLAB's user interface for creating scripts and functions
NEXT STEPS
  • Learn how to create and manage .m files in MATLAB
  • Explore MATLAB function scopes and contexts
  • Understand error handling in MATLAB programming
  • Investigate best practices for MATLAB function design
USEFUL FOR

MATLAB users, particularly those new to function definitions, programmers looking to improve their coding practices, and educators teaching MATLAB programming concepts.

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

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 11 ·
Replies
11
Views
3K