How can I pass additional parameters to a function I defined?
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello,
When using an odexx solver I know i can pass additional parameters following this structure (example for ode45)
[T,X] = ode45(f,tspan,x0,[],u,d,par)
where u,d and par are the additional parameters I am passing to ode45.
Is there any way to do the same for a function I defined? As an example, I have the following function
[t,x] = FoxRabbit_Euler_v1(f1,tspan,x0,n);
In this case f1 has some parameters I will like to pass when using "FoxRabbit_Euler_v1".
Any help will be very much appreciated
0 Kommentare
Akzeptierte Antwort
Ryan Livingston
am 7 Mär. 2013
You may want to have a look at varargin:
Defining a function like
function out = foo(a,b,varargin)
...
allows you to require a and b as arguments then allows the user to pass along anything else. These other arguments are put into the cell array varargin and can be retrieved from there:
if (nargin >= 3)
extraInput1 = varargin{1};
elseif (nargin >= 4)
extraInput2 = varargin{2};
...
end
4 Kommentare
Weitere Antworten (1)
Shashank Prasanna
am 7 Mär. 2013
You can use anonymous functions to achieve just that:
Although the above link is from the optimization toolbox the concept is the same and is a pretty standard way across matlab toolboxes to pass additional parameters.
The following should be helpful too:
Siehe auch
Kategorien
Mehr zu Creating, Deleting, and Querying Graphics Objects finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!