
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Creating Function in Files in MATLAB
MATLAB is a highlevel programming language used to perform engineering and scientific operations such as system design and analysis, matrix manipulation, data interpretation, image processing, app development, and more.
MATLAB provides several methods to write reusable codes that can be used to perform repeated tasks. One such code in MATLAB is a function. In this tutorial, I will explain how to create functions in a file in MATLAB. But before that let's have a look into some basic concepts related to MATLAB functions.
What is a Function in MATLAB?
In MATLAB, a function is a code or command or a set of instructions that takes inputs from user and produces a result as per the instructions.
It is basically a code block created to reuse multiple times to perform a particular task. Creating a function in MATLAB makes coding easier, reduces code errors and redundancy, and saves a lot of time required to rewrite the same code many times.
Types of Functions in MATLAB
In MATLAB, there are two types of functions namely,
Builtin Functions
UserDefined Functions
A builtin function is one which already defined in the library of the MATLAB software. Hence, it is also called library function. Examples of builtin functions are log, sqrt, readtable, writetable, disp, imshow, and more.
On the other hand, a userdefined function in MATLAB is a function whose definition is given by the user by writing MATLAB commands.
How to Create Function in MATLAB
MATLAB provides a standard way of creating a function by writing a piece of code. The stepbystep process to create a function in file in MATLAB is explained below:
Step (1) Launch MATLAB command window and create a new function file. For this, under "Home" tab, click on "New", and then select "Function".
Step (2) Define the signature of the function. This will take the following standard syntax:
function result = MyFun(input1, input2, input3,?) % Function body end
In this code, replace ?MyFun' with the name of your function. Specify the input arguments of the function.
Step (3) Inside the body of the function, write the code that performs the task that you want. For example,
function result = MyFun(input1, input2, input3) % Function body result = input1 * input2 * input3; % find product end
Step (4) Save the function file with the name same as the function name. For example, in this case ?MyFun.m'.
Now, we can call this function from the MATLAB command window to perform the function for which it defined.
Let us now consider some examples to understand how to create a function in MATLAB.
Example (1) Create a Function to calculate electric power in a circuit
Step (1) Create a function file and write the following code:
% Define the function to calculate power function electric_power = CalPower(voltage, current) % Expression to calculate power electric_power = voltage * current; end
Step (2) Save this function file with a name "CalPower.m".
Step (3) Open MATLAB command window and call the function ?CalPower' as follows:
% MATLAB code to call the CalPower function % Specify the values of voltage and current voltage = 220; % in volts current = 6; % in amperes % Call the CalPower function electric_power = CalPower(voltage, current); % Display the result fprintf('The electric power is: %.2f Watts', electric_power);
Output
The electric power is: 1320.00 Watts
Let us take another example of creating a function to calculate energy consumed by a lamp.
Example (2) Create a function to calculate electricity bill amount in MATLAB
Step (1) Create a function to calculate electricity bill amount:
function BillAmt = ElectricityBill(PowerWatt, WorkHours, RatePerUnit) % Expression to calculate total energy consumed in kilowatt-hours (units) Energy_Consumed = (PowerWatt * WorkHours) / 1000; % Calculate the electricity bill amount BillAmt = Energy_Consumed * RatePerUnit; end
Step (2) Save this function file with name "ElectricityBill".
Step (3) Open MATLAB command window and call the function ?ElectricityBill' as follows:
% MATLAB code to calculate electricity bill amount % Provide input parameters LampPower = 100; % in Watts WorkHours = 8; % lamp is used per day price = 6; % Rs. 6 per unit (adjust as per need) % Calculate the electricity bill amount bill_amount = ElectricityBill(LampPower, WorkHours, price); % Display the result fprintf('The electricity bill amount for the lamp is: %.2f INR per day', bill_amount);
Output
The electricity bill amount for the lamp is: 4.80 INR per day
Conclusion
In this tutorial, I have explained the concept of function and stepbystep process to create a function in MATLAB. Also, I have included some example programs to create functions for better understand of the steps involved in creating function in MATLAB.
In conclusion, MATLAB provides a standard method to create a function to perform a particular task. Functions are usually created to reduce code redundancy and to create reusable code block to save time to develop new codes.