Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Software Engineering Tutorial
  • Software Development Life Cycle
  • Waterfall Model
  • Software Requirements
  • Software Measurement and Metrics
  • Software Design Process
  • System configuration management
  • Software Maintenance
  • Software Development Tutorial
  • Software Testing Tutorial
  • Product Management Tutorial
  • Project Management Tutorial
  • Agile Methodology
  • Selenium Basics
Open In App
Next Article:
Function With Variable Number of Input Arguments in MATLAB
Next article icon

How to Validate Number of Function Arguments in MATLAB?

Last Updated : 26 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

MATLAB functions are generally defined input and output arguments. To validate the number of these input-output arguments, MATLAB provides us with the following functions:

  1. narginchk
  2. nargoutchk

Syntax:

narginchk(<minimum-inputs>, <maximum-inputs>)

nargoutchk(<minimum-outputs>, <maximum-outputs>)

We use narginchk and nargoutchk to manually handle the exceptions generated by a lesser or greater number of arguments during a function call. In the following sections, we will see the usage of both functions with examples.

NARGINCHK in MATLAB

As discussed above, narginchk is used to validate the number of input arguments. We shall see an example where, if the input arguments in function calls are in range 2,10 then, the function prints them all else, narginchk throws an error.

Example 1:

Matlab
% MATLAB Code fun(2,3,4) fprintf("Lees input variables!\n\n") fun()  % function function fun(varargin)     min = 2;     max = 10;     narginchk(min,max)     disp(varargin) end 

Output:

 

In this code, the function fun takes a variable number of input arguments with the help of the varargin argument. Then, the narginchk function checks whether the number of passed input arguments is in the range 2,10 or not. If true then, the function displays the variable argument list.

As it can be seen in the output, the first function call with 3 (>2 & <10) arguments print the argument list however, the second function call throws an error as the number of arguments is 0.

NARGOUTCHK in MATLAB

Similarly, we can validate the number of output arguments with the help of the nargoutchk function. narginchk throws an error when a number of output arguments is not in the defined range. See the following example where we assign a random number to each output argument whenever the number of output arguments is from 3 to 9.

Example 2: 

Matlab
% MATLAB Code [w,x,y,z] = fun(); disp([w x y z]) fprintf("Less output variables!\n\n") fun()  %function function [varargout] = fun     min = 3;     max = 9;     nargoutchk(min,max)     fprintf('Number of requested outputs is %d\n',nargout)     varargout = cell(nargout,1);     for i = 1:nargout         varargout{i} = rand;     end end 

Output:

 

In the above code, we define a function that assigns a random number to all of its output arguments and displays the number of output arguments, using the varargout for a variable number of outputs. The nargoutchk checks whether the number of output arguments is in the range 3,9. If not, it throws an error. So the first function call with 4 output arguments works as expected whereas the second call with no output arguments throws an error.


Next Article
Function With Variable Number of Input Arguments in MATLAB

O

owl0223
Improve
Article Tags :
  • Software Engineering
  • Technical Scripter 2022
  • MATLAB-Maths

Similar Reads

  • How to Find Number of Function Arguments in MATLAB?
    The number of function arguments passed in MATLAB will be determined in the following article. Unlike C, C++, and Java, MATLAB can accommodate a variable amount of parameters provided into a function without throwing an error. We'll go through how we determine the actual amount of parameters supplie
    4 min read
  • Function With Variable Number of Input Arguments in MATLAB
    MATLAB functions have the functionality that many other programming languages lack. A MATLAB function can take a variable number of input arguments, without the use of arrays or any additional functionality. The way to go about this in MATLAB is by using the varargin - which can be interpreted as VA
    2 min read
  • Function Argument Validation in MATLAB
    The basics of Function Argument is, " The functions receive certain inputs from the calling command which applies in function syntax and processes it. sometimes It may or may not produce output argument it dependence completely on the functions efficiency or the accuracy of your code." Function Argu
    5 min read
  • How to Validate Input to a Function Error in R
    In this article, we will examine various methods for how to validate input to the function by using R Programming Language. How to validate input to the function?Validating input to a function in R is crucial for ensuring that your code behaves as expected and can handle various types of input grace
    5 min read
  • How To Plot a Function of Two Variables in MATLAB?
    In order to plot a function of two variables in Matlab first you have to know some functions like plot, meshgrid() function, and very well know about how to plot one variable functions in Matlab. You have to follow some main contents or you can say procedure to plot a function. Below is the process
    2 min read
  • How to create a function in MATLAB ?
    A function is a block of statements that intend to perform a specific task. Functions allow the users to reuse the code frequently. MATLAB has several predefined functions which are ready to use such as sin(), fact(), cos() etc. MATLAB also allows the users to define their own functions. Syntax: fun
    2 min read
  • How to bind arguments to given values in Python functions?
    In Python, binding arguments to specific values can be a powerful tool, allowing you to set default values for function parameters, create specialized versions of functions, or partially apply a function to a set of arguments. This technique is commonly known as "partial function application" and ca
    3 min read
  • Use Custom Data Validation Function To Limit Number of Digits In Excel
    There might arise a situation while working on Excel where you want the users to type in 10-digits(say) numbers only in a cell. In these types of scenarios, we can make use of the Custom Data validation Function to limit the number of digits in Excel. In this article, we will discuss the same. Sampl
    1 min read
  • How to Find the Position of a Number in an Array in MATLAB?
    Finding the position of a number in an array, which can be done using the find() function. The find() function is used to find the indices and values of the specified nonzero elements. Syntaxfind(X) Parameters: This function accepts a parameter. X: This is the specified number whose position is goin
    1 min read
  • Function Argument Validation
    MATLAB is a programming language that is used for solving math problems so it is also a concept of MATLAB programming language. It is basically defined as a process to declare specific restrictions on the function arguments. By using argument validation we can constrain the class, size, and other th
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences