Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4021552
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:21:58+00:00 2026-05-20T10:21:58+00:00

I recently came to a problem that I was supposed to define a function

  • 0

I recently came to a problem that I was supposed to define a function with uncertain number of inputs, that is, the number of inputs may vary depending on the practical context. Should I take use of a 2-D array or something else? I don’t know if struct2cell helps and how if it really works.

Does anyone have an idea of the best way to go about doing this?

I’ve probably not been very clear, so please let me know if anything needs clarifying.

Thanks

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T10:21:58+00:00Added an answer on May 20, 2026 at 10:21 am

    There are several ways to go about this:

    Use optional input arguments if a given parameter means the same regardless of context, but if in some scenarios, additional input is needed.

    function out = myFun(first,second,third,fourth)
    
    %# first is always needed
    if nargin < 1 || isempty(first)
    error('need nonempty first input')
    end
    
    %# second is optional
    if nargin < 2 || isempty(second)
    second = defaultValueForSecondWhichCanBeEmpty;
    end
    
    %# etc
    

    You can call this function as out = myFun(1,[],2,3), i.e. pass an empty array for non-needed inputs.


    If two inputs mean that the function is used one way, and three inputs mean that the function is used in another way (and even the inputs mean different things), use VARARGIN

    function out = myFun(varargin)
    
    %# if 2 inputs, it's scenario 1, with 3 it's scenario 2
    switch nargin
    case 2
       firstParameter = varargin{1};
       secondParameter = varargin{2};
       scenario = 1;
    case 3
       firstParameter = varargin{1}; %# etc
    otherwise
       error('myFun is only defined for two or three inputs')
    end
    

    Finally, you can also have your inputs passed as parameterName/parameterValue pairs. See for example this question on how to handle such input.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently came across a big problem, as I have a system that's paying
Recently I came across a problem that asks to find out the non-unique characters
I recently came across an IE7 only bug that I thought I'd share so
I recently came across a ASP 1.1 web application that put a whole heap
Question So I've recently came up with some new possible projects that would have
I was working on some code recently and came across a method that had
An interesting issue came up recently. We came across some code that is using
Recently I wrote a function to generate certain sequences with nontrivial constraints. The problem
I recently came across a problem for image file storage in network. I have
I recently came across this problem and found a solution but I'm wondering if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.