VOOZH about

URL: https://www.geeksforgeeks.org/matlab/creating-function-in-files-in-matlab/

⇱ Creating Function in Files in MATLAB - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Creating Function in Files in MATLAB

Last Updated : 28 Apr, 2025

MATLAB is a high-performance language that is used for matrix manipulation, performing technical computations, graph plottings, etc. It stands for Matrix Laboratory. 

Functions:

The function is a set of statements or commands, which take input/s as parameters and return output. We write functions to use it again and again in the program i.e. it makes a block of code reusable. It makes it easier to find errors and also reduces code redundancy.

Types of Function:

  1. Library Functions: In MATLAB Software, there are some inbuilt functions, which take input as arguments and return output. E.g. log(x), sqrt(x), line space(a,b,n), etc.
  2. User-Defined Functions: In MATLAB, we also create functions by writing Matlab commands in files that take input/s as argument/s and then return the output. Also, save the function file using the name of the function itself. 

Steps to Create a Function File in MATLAB:

  1. Step 1: Create a file and write commands using the function keyword.
  2. Step 2: Save the function file with the same name as the function. Here I have saved the file with the name solve.m.
  3. Step 3: Use the function in Command Video by providing suitable argument/s.

Now we see creating a function and saving it in MATLAB.

Example 1:

Using the function to be called in Command Window.

Example 2:

Output:

👁 Image
 

The above screenshot is showing:

  • Function saved as a file named solve.m
  • Command Window in which we are calling function for various inputs.
Comment