calling a function in another function by changing only few input arguments

5 次查看(过去 30 天)
I have written a function function
[ yp,PAC ] = PAgent( c5,c6,c7,rcapa,Aineq5,Bineq5,Aeq5,Beq5,lb,ub,options )
suppose if i have to call this function in another function by changing only the input argument 'rcapa', i.e., other input arguments c5,c6,c7.... will not change, but only 'rcapa' changes, can i write like the following,
PA1 = @(rcapa) PAgent([],[],[],rcapa,[],[],[],[],[],[],[] );
[yp1,PAC1]= PA1(rcapa);

采纳的回答

Cedric
Cedric 2017-10-17
编辑:Cedric 2017-10-17
If you are in another function where variables with the same names are defined (e.g. because they are arguments of the function), but you want to create a wrapper for PAgent as a function of 1 parameter rcapa that calls another function with the same parameters but with this one updated, do it this way:
function otherFunction( c5,c6,c7,rcapa,Aineq5,Bineq5,Aeq5,Beq5,lb,ub,options )
PA1 = @(rcapa) PAgent( c5,c6,c7,rcapa,Aineq5,Bineq5,Aeq5,Beq5,lb,ub,options ) ;
then you can call PA1 with the specific value of rcapa, but all the other arguments will remain the same as defined by/in otherFunction.
This is like creating an internal function. All variables that are not defined locally come from the nesting/external function. The only internal/local variable in
PA1 = @(rcapa) PAgent( c5,c6,c7,rcapa,Aineq5,Bineq5,Aeq5,Beq5,lb,ub,options ) ;
is rcapa.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by