EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Matlab Tutorial Varargin in Matlab
 

Varargin in Matlab

Priya Pedamkar
Article byPriya Pedamkar

Updated March 3, 2023

Varargin in Matlab

 

 

Introduction to Varargin in Matlab

MATLAB is a programming environment that is interactive and is used in scientific computing. It is extensively used in a lot of technical fields where problem-solving, data analysis, algorithm development, and experimentation is required. The software which are discipline-specific is extensively written using MATLAB.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

In this article, we will study how to use ‘varargin’ input variable in MATLAB.

Before we start learning how ‘varargin’ works in MATLAB, let us understand why we need ‘varargin’ input variable.

VARARGIN

We use ‘varargin’ inside definition of a function so that we can pass as many arguments as we need. In some situations, it becomes difficult for a coder to decide the number of input arguments upfront. These situations require a function definition to be flexible enough to accept any number of arguments while calling it.

Let us now understand the syntax of ‘varargin’ in MATLAB:

Syntax:

varargin

[Please keep in mind that ‘varargin’ is in lowercase and passed as the last argument to the function]

Description:

  1. varargin is passed as an input variable to a function definition
  2. It provides the function with the ability to accept as many arguments as the coder wants.

Examples of Varargin in Matlab

Let us now understand how the code for ‘varargin’ looks like in MATLAB, with the help of various examples.

Example #1

In this example, we will define a function that will simply display the number of arguments passed. We will need to follow the following 2 steps:

  1. Create a file with a function stored in it. This will have ‘.m’ extension
  2. Call the function created in the above file

Code to create the function and save it as .m extension:

function learningVarargin (varargin)

[Defining the function learningVarargin and passing the ‘varargin’ command]

disp ("Input arguments passed: " + nargin)

[‘nargin’ is used to get the number of input variables]

celldisp (varargin)

[‘celldisp’ will display all the input variables]

end

[After creating the function, save this file by the name of your choice, here we are naming our file as ‘learningVarargin.m]

Now in the new command window, use the following code:

Code to call the function

type learningVarargin
learningVarargin ('extra argument', rand (3))

[Calling the learningVarargin function using arguments of our choice. Here we have used 2 arguments to call the function]

Input 1 (Creating the function):

function learningVarargin (varargin)
disp ("Input arguments passed: " + nargin)
celldisp (varargin)
end

Input 2 (Calling the function):

type learningVarargin
learningVarargin ('extra argument', rand (3))

Output:

Varargin in Matlab 1

Example #2

In this example, we will define a function that will plot a cos wave. We will pass the number of arguments to format the style of a plot as per or requirement.

Code to create the function and save it as .m extension:

function learningVarargin1 (x, varargin)

[Defining the function learningVarargin1 and passing the ‘varargin’ command]

plot (x, varargin {:})

[plot is used to get the graph]

varargin {:} will help us to pass as many arguments as we want to the plot]

end

[After creating the function, save this file by the name of your choice, here we are naming our file as ‘learningVarargin1.m]

Code to call the function

type learningVarargin1
learningVarargin1 (cos (0 : pi/200 : 4*pi), 'color', 'r', 'linestyle', ':')

[Calling the learningVarargin1 function using arguments of our choice. Here we have used ‘color’ and ‘linestyle’ argument in addition to the mandatory argument ‘x’, to call the function]

Input 1 (Creating the function):

function learningVarargin1 (x, varargin)
plot (x, varargin {:})
end

Input 2 (Calling the function):

type learningVarargin1
learningVarargin1 (cos (0 : pi/200 : 4*pi), 'color', 'r', 'linestyle', ':')

Output:

Varargin in Matlab 2

Example #3

In this example, we will define a function which will plot a sine wave. We will pass different arguments from the ones passed above, to format the style of plot.

Code to create the function and save it as .m extension:

function learningVarargin2 (x, varargin)

[Defining the function learningVarargin2 and passing the ‘varargin’ command]

 [Here, notice that the syntax for the function remains essentially the same. This is the power of ‘varargin’ command which will help us change the number of input arguments while calling the function, without worrying about the number of arguments]

Code to call the function

type learningVarargin2
learningVarargin1 (sine (0 : pi/200 : 4*pi), 'color', 'r')

[Calling the learningVarargin2 function using arguments of our choice. Here we have used only color argument in addition to the mandatory argument ‘x’, to call the function]

Input 1 (Creating the function):

function learningVarargin2 (x, varargin)
plot (x, varargin {:})
end

Input 2 (Calling the function):

type learningVarargin2
learningVarargin1 (sine (0 : pi/200 : 4*pi), 'color', 'b')

Output:

Varargin in Matlab 3

Conclusion

‘varargin’ can be used in MATLAB when we are not sure how many arguments we might need while calling the function. The flexibility it offers becomes very useful when using functions like plot, where we might need to format the graph on the go.

Recommended Articles

This is a guide to Varargin in Matlab. Here we discuss Introduction, syntax, Description, and Examples with code implementation. You can also go through our other related articles to learn more –

  1. uigetfile Matlab
  2. feval Matlab
  3. MATLAB Toolbox
  4. MATLAB Colon

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*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

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Limited Time Offer! - ENROLL NOW