MATLAB: “One or more output arguments not assigned during call to “varargout””

output argumentsvarargout

Hi everyone,
I'm currently working with a complex model that contains many interconnected functions.
1) At some point, I call the function rk4.m:
[TS.T, rk1_T, V1T, H1T, X1T, rk2_T, V2T, H2T, X2T, rk3_T, V3T, H3T, X3T, rk4_T, V4T, H4T, X4T, srf_Tflx1, sol_Tflx1, srf_Tflx2, sol_Tflx2, srf_Tflx3, sol_Tflx3, srf_Tflx4,...
sol_Tflx4] = rk4(@mixtracerTS_ode, it, TS.T, In, Grd, VMxng.Mld, VMxng.Entrnmnt, LB.tmp, TS.Sig, Wnd.Tauy2, P, 'sflux', Ht.Qi, Ht.airtmp, Ht.dewptT, Wnd.Wspd10,...
Ht.Qo,'source',Ht.Qi);
rk4.m is defined as follows:
function [y1, k_1, V1, H1, X1, k_2, V2, H2, X2, k_3, V3, H3, X3, k_4, V4, H4, X4, varargout] = rk4(ode, it, y0, In, Grd, varargin)
2) Inside rk4.m, I call several times the function ode / mixtracerTS_ode.m. For instance, the first time:
[k_1, V1, H1, X1, srf_Tflx1, sol_Tflx1] = ode(Grd.time(it), y0, In, Grd, varargin {:});
mixtracerTS_ode.m is defined as follows:
function [dxdt, V, H, X, varargout] = mixtracerTS_ode(time, tracer, In, Grd, mld, ent, lb, Sig, tauy, prate, varargin)
a) mandatory output arguments are calculated thanks to other functions:
V = verticalmixing(tracer, Grd, mld, ent, In, time);
H = horizontalmixing(tracer, Grd, mld, lb, In, time);
X = advection(tracer, lb, In, Grd, mld, tauy, Sig, prate, time);
dxdt = V+ H+ X;
b) portion to calculate varargout / srf_Tflx1 & sol_Tflx1:
nArgs=length(varargin);
if nArgs >0
opt = varargin(cellfun(@ischar,varargin));
if any(ismember(opt, {'sflux'}))
Ht.Qi=varargin{2};
Ht.airtmp=varargin{3};
Ht.dewptT=varargin{4};
Wnd.Wspd10=varargin{5};
Ht.Qo=varargin{6};
.......
.......
calculation of srf_Tflx
......
varargout{1}=srf_Tflx;
end
opt = varargin(cellfun(@ischar,varargin));
if any(ismember(opt, {'source'}))
Ht.Qi=varargin{8};
......
......
calculation of sol_Tflx
.......
.......
varargout{2}=sol_Tflx;
end
end
I get this error message:
One or more output arguments not assigned during call to "varargout"
Error in rk4 (line 39)
[k_1, V1, H1, X1, srf_Tflx1, sol_Tflx1] = ode(x(it), y0, In, Grd, varargin {:});
I really can't figure this out especially because everything works fine when I run my functions from the command window with appropriate inputs!! For example, the first function to be called
[TS.T, rk1_T,……, sol_Tflx4]=rk4(@mixtracerTS_ode, …. , Ht.Qi) gives me the expected outputs. But when I run the entire model, I get the above error message.
Any help from you would be much appreciated !!
Many thanks in advance.

Best Answer

rk4 line 39, you always call ode with 6 outputs, but the inputs can vary because of the varargin{:}
WCVOE2E_physicalmodel, line 378, you call rk4 passing in both 'source' and 'sflux' options and expecting 25 outputs.
Line 384, you call rk4 without 'source' or 'sflux' options and expecting 17 outputs.
mixtracerTS_ode line 1 defines 4 constant outputs and varargout. With it being called by rk4 with 6 outputs each time, varargout{1} and varargout{2} will need to be set each time.
mixtracerTS_ode.m lines 82-83 checks nargin . In both calls (line 378 vs line 384) nargin is non-empty, so 'sflux' is checked for. The call on line 378 (the first call) provided it so the if on line 86 of mixtracerTS_ode.m gets control and assigns to varargout{1} on line 122; similarly with 'source' having been passed, the if on line 132 has control and assigns to varargout{2} on line 174. But the call on line 384 (the second call) provided neither sflux nor source, so the assignments at line 122 and 174 are not executed, and varargout{1} and varargout{2} are not assigned to, leaving you two outputs short for the call to ode() on line 39 of rk4.
Related Question