Scripts and Functions in MATLAB
Last Updated :
28 Apr, 2025
In MATLAB there are a different kinds of files dedicated to MATLAB codes. They are the following:
- Script
- Live Script
- Function only file
- Class file
Now only the live script is the only one of these which has a different extension name; all other three use the standard .m extension. In this article, we shall compare the script and function files.
Scripts in MATLAB
A script file is an ordinary MATLAB file that could contain any code except a class definition. See the following example which creates and displays a magic square.
Example 1:
Matlab
% MATLAB Code
mag = magic(5);
disp(mag)
We create a script file named geeks.m and write the above code into it. Output:
Now, a script file can also contain a function definition in it. The syntax for the same is
---code---
--- Function Definitions---
It is mandatory that the function definitions must be written after all the codes in the script.
While declaring functions in a script file, keep the following things in mind:
- Function definitions must be written after all code in script file.
- File name does not necessarily need to be same as function name.
- All the declared functions will be local functions to that script.
In the following example, we will create a function to calculate the factorial of a given number and call it in the same script.
Example 2:
Matlab
% MATLAB code
fac = fact(5)
%function to calculate factorial
function y=fact(n)
if(n<=0)
y=1;
else
y=n*fact(n-1);
end
end
Output:
Functions in MATLAB
As seen in the previous section, a script file can contain a locally declared function. Now, traditionally a function in MATLAB is defined more globally by creating it in its specified file. A file that contains a function only needs to fulfill following conditions:
- Only one parent function is allowed per file.
- The file name must be the same as the function name.
See the following example in which we create a function 'geeks' in a file named geeks.m. This function will return the URL of GeeksForGeeks.
Example 3:
Matlab
% MATLAB code for
% function
function y=geeks
y="https://www.geeksforgeeks.org";
end
Output:
As it can be seen that the file name is same as the function name, and it is globally accessible.
Similar Reads
Add Functions to Scripts in MATLAB From MATLAB version 2016b, it is possible to add functions directly into a script or live script. In this article, we shall how to add functions to script files. Â The syntax is simple except one rule that the function body must be written after the codes in the script. statement 1 statement 2 . stat
2 min read
Private Functions in MATLAB Private functions are useful when you want to limit the scope of a function. Here we will learn how to create private functions and also use them. Private functions are primary functions that are visible only to a limited group of other functions. Generally, we make private functions, if we want to
2 min read
Find() function in MATLAB The find() function in MATLAB is used to find the indices and values of non-zero elements or the elements which satisfy a given condition. The relational expression can be used in conjunction with find to find the indices of elements that meet the given condition. It returns a vector that contains t
3 min read
User defined function in MATLAB Functions let you do a specific task. User defined functions are the functions created by the users according to their needs. This article explains how the user defined function in MATLAB is created. Syntax : function [a1,...,an] = func(x1,...,xm) func is the function name a1,...,an are outputs x1,.
2 min read
Nested Functions in MATLAB Functions in any programming language are some blocks of code, which could be reused whenever required, by just calling the name. It reduces so much of human effort and also rewriting of the same code, and makes the entire code big. Declaring a Function: To declare a function in MATLAB we use given
2 min read
Functions in MATLAB Methods are also popularly known as functions. The main aim of the methods is to reuse the code. A method is a block of code which is invoked and executed when it is called by the user. It contains local workspace and independent of base workspace which belongs to command prompt. Let's take a glance
5 min read