Section 5.8 helps you to identify the possible cause(s) for an error message that is being gener-
ated by your MATLAB code as follows:
1. When you receive an error message as follows:
??? Error: File: A function declaration cannot appear within a script
M-file.
The possible cause is that you have defined a function inside a script file as shown in the
following script file (try1.m):
try1.m
x = 2;
y=square(x)
function out_var = square(x)
out_var = x*x;
A function can never be defined in a script file. MATLAB does not execute a script file if it
finds a function declaration within it.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month, and much more.