• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
EDUCBA

EDUCBA

MENUMENU
  • Resources
        • Data & Analytics Career

          • Big Data Analytics Jobs
          • Hadoop developer interview Questions
          • Big Data Vs Machine Learning
        • Data and Analytics Career
        • Interview Questions

          • Career in Cloud Computing Technology
          • Big Data interview questions
          • Data Scientist vs Machine Learning
        • Interview Questions
        • Machine Learning

          • What is Machine Learning
          • Machine Learning Tools
          • Neural Network Algorithms
        • Head to Head Differences
        • Others

          • Resources (A-Z)
          • Data and Analytics Basics
          • Business Analytics
          • View All
  • Free Courses
  • All Courses
        • Certification Courses

          Data Science Course
        • All in One Bundle

          All-in-One-Data-Science-Bundle
        • Machine Learning Course

          Machine-Learning-Training
        • Others

          • Hadoop Certification Training
          • Cloud Computing Training Course
          • R Programming Course
          • AWS Training Course
          • SAS Training Course
          • View All
  • 360+ Courses All in One Bundle
  • Login

MATLAB Functions

Home » Data Science » Blog » Data Analytics Basics » MATLAB Functions

MATLAB Functions

Introduction to MATLAB Functions

MATLAB Functions are written with various lines of code which relate one variable with another variable and each output is related exactly to one particular input that forms an important part of any programming language. In MATLAB environment, they are stored in a certain file like script files, etc. They can accept more than one input argument and can also return more than one output argument. They operate on the defined variables in their predefined workspace, known as the local workspace.

Types of Functions in MATLAB

In Matlab, functions can be created or defined  using the below syntax:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

function [o1,o2, ..., oN] = myfun(i1,i2,i3, ..., iN)

 Here my fun is the function name which accepts the input arguments i1,i2..iN and returns output o1,o2…on. The above statement should be first-line while declaring the function. There are some rules which need to be followed for valid function names and saving the function:

  • Function name which begins with alphabets and can contain number, characters or underscores are considered valid.
  • We can save the function in a function file that contains function definitions and the name of the file must match the first function name in the file.
  • We can also save the function which contains function definitions and commands. Functions should be present at the end of the file and script file name cannot have the same name as the function in the file.

The end keyword should be used to indicate the end of the function. It is required when any function in the file contains a nested function or function used is a local function within the script and function file. Functions work in scripts in R2016b or later. There are several types of functions used in MATLAB. They are as follows:

1. Anonymous Function

It is the function that is not stored in a program file but it is associated with the variable whose data type is function_handle. It is defined in a single statement and has any number of input or output arguments. The syntax can be written as:

Fun=@(argumentlist)expression

Example:

mul=@(x,y) x*y;
res1=mul(2,3)
res2=mul(4,3)
res3=mul(4,5)

When we run the output we get the following result:

Popular Course in this category
Data Scientist Training (76 Courses, 56+ Projects) 76 Online Courses | 56 Hands-on Projects | 626+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.8 (3,610 ratings)
Course Price

View Course

Related Courses
Machine Learning Training (17 Courses, 20+ Projects)Cloud Computing Training (18 Courses, 4+ Projects)

res1=6

res2=12

res3=20

We can write anonymous functions with no inputs or multiple inputs and outputs. If the function has no input then we can use empty parenthesis to call the anonymous function. Some of the examples are

1. curr= @() datestr(now);
d = curr()
Output: d= 22-Oct-2019 11:02:47

2. myfun=@(x,y)(x+y);
x=4
y=7
z=myfun(x,y)
Output: z=11

2. Local Functions

Any function file contains a primary function that appears in the first line of the code. They are visible to functions in other files and can be called in the command line. Other functions that are present in the file are called local functions. They cannot be called from the command line and are seen to a parent or main function and functions written in the same file. They are also known as sub-functions. They are equivalent to subroutines as used in another programming language. They can also be written in script files as long as they appear after the last line of the script code.

function [avg, med] = mystats(y)
a= length(y);
avg = mymean(y,a);
med = mymedian(y,a);
end

function a = mymean(v,n) ---- Example of a local function
a = sum(v)/n;
end

3. Nested Functions

Functions that are defined within another function or parent function are called nested functions. They can use or modify the variables that are defined in the parent function. They are defined within the scope of the function and can access the workspace in which they are defined. There are certain requirements which every nested function should follow:

  • All the functions do not require the end statement. However, to nest any function end statement should be written for every function.
  • We cannot define nested functions inside any control statements like if-else, switch case, etc.
  • Nested functions can be called directly by name or using any function handle.

function current
nestfun1
nestfun2
function nestfun1
x = 7;
end
function nestfun2
x = 4;
end

4. Private Functions

They are the functions that are visible only to a limited group of functions. They reside in sub-functions and are denoted using keyword “private”. They are only visible to functions that are present in the parent folder or the functions in the folder immediately above the private subfolder. They are useful when we want to limit the scope of the function. We cannot call the private functions from the command line or from the functions outside of the parent folder.

Code:

function priv
disp(“Hello “)

Change the folder that contains a private folder and change the file name to present
function present
priv

Change the folder to any location and call the present function
present

Output:” Hello”

Conclusion – MATLAB Functions

 MATLAB Functions can be used for several tasks and scenarios. They form an integral part of any programming language. They can be accessed both globally by using global variables or using private functions if we want some information or content to be private. They are used in every organization for the business needs to fulfill the demands.

Recommended Articles

This is a guide to the MATLAB Functions. Here we discuss the Introduction of the MATLAB Functions and the Types of Functions. You can also go through our other suggested articles to learn more –

  1. Bessel Functions in MATLAB
  2. Anonymous Functions in Matlab
  3. Math Functions in C#
  4. Transfer Functions in Matlab
  5. Matlab Compiler | Applications of Matlab Compiler

All in One Data Science Bundle (360+ Courses, 50+ projects)

360+ Online Courses

50+ projects

1500+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Reader Interactions
Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar
Data Analytics Tutorials Tutorials
  • Data Analytics Basics
    • Data Warehouse Design
    • Oracle Queries
    • Git Life Cycle
    • 3D Matrix in MATLAB
    • Sensor Device
    • GIT Cherry-pick
    • What is Proximity Sensors?
    • Types of Trees in Data Structure
    • VPN Applications for PC
    • Ansible vs Puppet vs Chef
    • Matlab Compiler
    • GIT Version Control System
    • Git Push
    • Random Number Generator in Matlab
    • What is Inkscape?
    • Resolve Merge Conflict in GIT
    • Sum Function in Matlab
    • Mainframe Testing
    • What is Backlink in SEO?
    • Multidimensional Database
    • Git Origin Master
    • Piecewise Function in Matlab
    • Joins in Oracle
    • Talend Tools
    • Informatica vs Datastage
    • Git ReBase vs Merge
    • Operations in OLAP
    • Advantages of Matlab
    • MATLAB Functions
    • Types of OLAP
    • Qlikview Dashboard
    • How to use Google Analytics?
    • IF-Else Statement in Matlab
    • Internal Linking in SEO
    • QlikView Charts
    • QlikView Alternatives
    • Oracle Data Warehousing
    • ROLAP vs MOLAP vs HOLAP
    • Git Checkout Command
    • Mean Function in Matlab
    • GitHub Clone
    • Normalizer Transformation in Informatica
    • Filter Function in Matlab
    • Mobile SEO
    • What is Head in Git?
    • Talend Open Studio
    • Cloud Monitoring Tools
    • Matrix in Matlab
    • Data Science Techniques
    • Linear Regression Analysis
    • Advantages of Azure
    • Benefits of Data Warehouse
    • Best Cloud Hosting
    • Bessel Functions in MATLAB
    • TensorBoard
    • Git Tools
    • What is Data Cube?
    • Git Checkout Tag
    • Vectors in Matlab
    • Data Science Skills
    • Anonymous Functions in Matlab
    • Best Data Science Programs
    • Docker Architecture
    • For Loop in R
    • Two Way ANOVA in R
    • Git Fetch vs Git Pull
    • MATLAB Version
    • Skills Required for Data Scientist
    • Inline Functions in Matlab
    • R Data Frame
    • Types of Data Analysis Techniques
    • What is Genetic Algorithm?
    • Transfer Functions in Matlab
    • What is Git Branch?
    • Git Terminology
    • What is Dropbox?
    • Career in Business Intelligence
    • What is Off-Page SEO
    • Informatica Architecture
    • R CSV Files
    • Data Scientist Skills
    • SVM Algorithm
    • What is Fact Table?
    • What is Google Analytics?
    • LOOKUP Function in Tableau
    • Types of Artificial Intelligence
    • What is AWS RedShift?
    • RPA Tools
    • How to Install Kubernetes
    • Docker Commands
    • Docker Commands Cheat Sheet
    • Docker vs VMs
    • What is Matlab
    • Clustering Algorithm
    • Business Intelligence Tools
    • Binomial Distribution in R
    • Scatterplot in R
    • Kernel Methods
    • Linear Regression in R
    • Data Science Tools
    • PowerShell String Functions
    • What is Supervised Learning?
    • IoT Standards
    • OpenShift Alternatives
    • While Loop in Matlab
    • Data Science Platform
    • Pie Chart in R
    • Logistic Regression in R
    • Decision Tree in R
    • Data Analysis Tools
    • Line Graph in R
    • Matlab Commands
    • IoT Framework
    • IoT Services
    • Arrays in R
    • Bar Charts in R
    • What is RDBMS?
    • What is GTM?
    • Histogram in R
    • Loops in R
    • Data Types in MATLAB
    • SAS Operators
    • SAS Alternatives
    • Crowdsourcing Data Strategies
    • Data Supply Chain
    • Your Analytics Software
    • Data Modeling Tools
    • Data Warehouse Tools
    • MATLAB - Powerful Technical Computing
    • Data Science and Its Growing Importance
    • Fraud Detection Analytics
    • Data Analysis Tools For Research
    • Data Analytics Trends in 2016
    • R Tools Technology
    • Create Data Exploration in R
    • Informatica Developer Tool
    • Data Science vs Software Engineering
    • Talend Vs Informatica PowerCenter
    • Data Science vs Data Analytics
    • Data Science vs Statistics
    • Data Science vs Web Development
    • Data Analytics Vs Predictive Analytics
    • Talend vs Pentaho
    • Talend vs SSIS
    • Cassandra vs Elasticsearch
    • Cassandra vs Redis
    • Data Analyst vs Data Scientist
    • Raspberry Pi with a Mix of Python
    • Uses Of Matlab
    • SAS vs RapidMiner
    • SAS vs SSD
    • Introduction to IOT
    • What is RDD
    • How To Install Apache
    • Is Matlab Free
    • How to Install Spark
    • How to Install MATLAB
    • Introduction to Blockchain
    • Advantages of Blockchain
    • Matlab Operators
    • What is Data Science
    • Docker Alternatives
    • Matlab Alternatives
    • What is Docker in Linux
    • What is Data Analytics
    • How To Install Hive
    • What is Apache Flink?
    • What is Predictive Analytics
    • Hadoop vs MongoDB
    • What is Business Intelligence
    • Best Web Analytics Tools
    • Best Out of Your Customer Data
    • What is OLAP
    • How to Connect Database in Java
    • IOT Tools
    • What is NumPy
    • What is Data Processing
    • Predictive Modeling
    • Data Warehouse vs Data Mart
    • Data Warehouse Architecture
    • Data Warehouse vs Database
    • OLTP vs OLAP
    • OLAP Tools
    • Data Warehouse Tools
    • ROLAP vs MOLAP
    • Data Integration Tool
    • What is DSS
    • Continuous Integration Tools
    • Types of Data Warehouse
    • Kubernetes vs Docker
    • Fact Table vs Dimension Table
    • Switch Statement in Matlab
    • IoT Protocols
    • Transformations in Informatica with Example
    • Career in SAS
    • Careers In Informatica
    • SAS vs R
    • TensorFlow vs Caffe
    • What is Informatica
    • Data vs Information
    • What is Data Warehouse
    • What is Open Cart?
  • Big Data (151+)
  • Business Analytics (40+)
  • Cloud Computing (82+)
  • Data Analytics Careers (36+)
  • Data Mining (30+)
  • Data Visualization (88+)
  • Interview Questions (50+)
  • Machine Learning (141+)
  • Statistical Analysis (36+)
  • Data Commands (4+)
  • Power Bi (6+)
Data Analytics Tutorials Courses
  • Data Science Certification
  • Online Machine Learning Training
  • Cloud Computing Certification
Footer
About Us
  • Who is EDUCBA?
  • Sign Up
  •  
Free Courses
  • Free Course on Data Science
  • Free Course on Machine Learning
  • Free Coruse on Statistics
  • Free Course on Data Analytics
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course
  • Tableau Training
  • Azure Training Course
  • IoT Course
  • Minitab Training
  • SPSS Certification Course
  • Data Science with Python Course
Resources
  • Resources (A To Z)
  • Data & Analytics Career
  • Interview Questions
  • Data Visualization
  • Data and Analytics Basics
  • Cloud Computing
Apps
  • iPhone & iPad
  • Android
Support
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions

© 2019 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

Let’s Get Started
Please provide your Email ID
Email ID is incorrect

Limited Period Offer - All in One Data Science Bundle (360+ Courses, 50+ projects) View More

Limited Period Offer - Limited Period Offer - All in One Data Science Bundle (360+ Courses, 50+ projects) View More