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.
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,evalinandassignin).Next to
global,evalinand 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)(Ais the amplitude,Tthe period). In MATLAB one would implement this as:When calling this function, we need to provide all parameters. If we extend this function to something like the following, we can omit the
AandTparameters:This uses the construct
nargin, if you want to know more, it is worthwhile to consult the MATLAB help fornargin,varargin,varargoutandnargout. However, do note that you have to provide a value forAwhen you want to provide the value ofT. There is a more convenient way to get even better behavior:This has the benefits that it is more clear what is happening and you could omit
Abut still specifyT(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 callingsinewave(1:10,[],4)whereAwill retain it’s default value. If an empty input should be valid, you should use another invalid input (e.g.NaN,infor a negative value for a parameter that is known to be positive, …).Using the function above, all the following calls are equivalent:
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
sinewavefunction 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:You can work with f as you would with an other function:
e.g.
f(10)will evaluatesinewave(10,1,1).That way you can write a general function (i.e.
sinewavethat 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.