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 6530767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:47:44+00:00 2026-05-25T09:47:44+00:00

when I am doing a function in Matlab. Sometimes I have equations and every

  • 0

when I am doing a function in Matlab. Sometimes I have equations and every one of these have constants. Then, I have to declare these constants inside my function. I wonder if there is a way to call the values of that constants from outside of the function, if I have their values on the workspace.

I don’t want to write this values as inputs of my function in the function declaration.

  • 1 1 Answer
  • 0 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-25T09:47:44+00:00Added an answer on May 25, 2026 at 9:47 am

    In addition to the solutions provided by Iterator, which are all great, I think you have some other options.

    First of all, I would like to warn you about global variables (as Iterator also did): these introduce hidden dependencies and make it much more cumbersome to reuse and debug your code. If your only concern is ease of use when calling the functions, I would suggest you pass along a struct containing those constants. That has the advantage that you can easily save those constants together. Unless you know what you’re doing, do yourself a favor and stay away from global variables (and functions such as eval, evalin and assignin).

    Next to global, evalin and passing structs, there is another mechanism for global state: preferences. These are to be used when it concerns a nearly immutable setting of your code. These are unfit for passing around actual raw data.

    If all you want is a more or less clean syntax for calling a certain function, this can be achieved in a few different ways:

    You could use a variable number of parameters. This is the best option when your constants have a default value. I will explain by means of an example, e.g. a regular sine wave y = A*sin(2*pi*t/T) (A is the amplitude, T the period). In MATLAB one would implement this as:

    function y = sinewave(t,A,T)
    y = A*sin(2*pi*t/T);
    

    When calling this function, we need to provide all parameters. If we extend this function to something like the following, we can omit the A and T parameters:

    function y = sinewave(t,A,T)
    if nargin < 3
       T = 1; % default period is 1
       if nargin < 2
          A = 1; % default amplitude 1
       end
    end
    y = A*sin(2*pi*t/T);
    

    This uses the construct nargin, if you want to know more, it is worthwhile to consult the MATLAB help for nargin, varargin, varargout and nargout. However, do note that you have to provide a value for A when you want to provide the value of T. There is a more convenient way to get even better behavior:

    function y = sinewave(t,A,T)
    if ~exists('T','var') || isempty(T)
       T = 1; % default period is 1
    end
    if ~exists('A','var') || isempty(A)
       A = 1; % default amplitude 1
    end
    y = A*sin(2*pi*t/T);
    

    This has the benefits that it is more clear what is happening and you could omit A but still specify T (the same can be done for the previous example, but that gets complicated quite easily when you have a lot of parameters). You can do such things by calling sinewave(1:10,[],4) where A will retain it’s default value. If an empty input should be valid, you should use another invalid input (e.g. NaN, inf or a negative value for a parameter that is known to be positive, …).

    Using the function above, all the following calls are equivalent:

    t = rand(1,10);
    y1 = sinewave(t,1,1);
    y2 = sinewave(t,1);
    y3 = sinewave(t);
    

    If the parameters don’t have default values, you could wrap the function into a function handle which fills in those parameters. This is something you might need to do when you are using some toolboxes that impose constraints onto the functions that are to be used. This is the case in the Optimization Toolbox.

    I will consider the sinewave function again, but this time I use the first definition (i.e. without a variable number of parameters). Then you could work with a function handle:

    f = @(x)(sinewave(x,1,1));
    

    You can work with f as you would with an other function:

    e.g. f(10) will evaluate sinewave(10,1,1).

    That way you can write a general function (i.e. sinewave that is as general and simple as possible) but you create a function (handle) on the fly with the constants substituted. This allows you to work with that function, but also prevents global storage of data.

    You can of course combine different solutions: e.g. create function handle to a function with a variable number of parameters that sets a certain global variable.

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

Sidebar

Related Questions

I'm doing some data analysis in Matlab, and anytime I call the hold function
I have a very simple CLR Function for doing Regex Matching public static SqlBoolean
If I use the load function by matlab I normally end up doing something
I have noticed that matlab does some matrix function really fast for example adding
I have some differential equations that I need to solve using MATLAB's ODE solvers.
i have this problem, doing: function GenGuid: String; var guid: TGuid; begin CreateGuid(Guid); Result
What's the difference between the functions plot and line in MATLAB? Are they doing
I doing a function in Javascript like the VisualBasic DateDiff. You give two dates
i'm doing a function in gwt it sends an IQ stanza into a server
I'm currently doing a highlighting function on a webpage and im using the jquery

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.