S-Function error "during flag=3 call must be a real vector of length 1"

Hello, I'm a beginner on Mathlab and I have an error with my S-Function on Simulink. When I execute the fuction, the error "during flag=3 call must be a real vector of length 1" appear. I look about the value that the function return but it isn't a complex value. Does anybody has an answer to this error?
function [sys,x0,str,ts] = Controllers_3PI_OnePR(t,x,u,flag,Ts)
global ts
wn=2*pi*50;
switch flag,
%%%%%%%%%%%%%%%%%%
% Initialization %
%%%%%%%%%%%%%%%%%%
case 0,
[sys,x0,str,ts]=mdlInitializeSizes;
%%%%%%%%%%%%%%%
% Derivatives %
%%%%%%%%%%%%%%%
case 2,
sys=mdlUpdate(t,x,u,wn,Ts);
%%%%%%%%%%%
% Outputs %
%%%%%%%%%%%
case 3,
sys=mdlOutputs(x);
%%%%%%%%%%%%%%%%%%%
% Unhandled flags %
%%%%%%%%%%%%%%%%%%%
case {1,4,9},
sys = []; % do nothing
%%%%%%%%%%%%%%%%%%%%
% Unexpected flags %
%%%%%%%%%%%%%%%%%%%%
otherwise
error(['unhandled flag = ',num2str(flag)]);
end
%
%=============================================================================
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the S-function.
%=============================================================================
%
function [sys,x0,str,ts]=mdlInitializeSizes
sizes = simsizes; sizes.NumContStates = 0; sizes.NumDiscStates = 3; sizes.NumOutputs = 1; sizes.NumInputs = 8; sizes.DirFeedthrough = 1; sizes.NumSampleTimes = 1;
sys = simsizes(sizes);
x0 = zeros(sizes.NumDiscStates,1); str = []; ts = [-1 0]; % Inherited sample time
% end mdlInitializeSizes % %============================================================================= % mdlDerivatives % Return the derivatives for the continuous states. %============================================================================= % function sys=mdlUpdate(t,x,u,wn,Ts) % States Updating E_ref = x(1); integral = x(2); Esource_filt = x(3);
% Inputs E_Ref_in = u(1); E_Meas = u(2); I_Meas = u(3); N_mod = u(4); Balance_ENABLE = u(5); VArm_kp = u(6); VArm_ki = u(7); fcut_Vtot = u(8);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Balance control %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Mean voltage : sum of all individual voltages divided by N, Ref is % Filtering voltages k = exp(-2.0*pi*fcut_Vtot*Ts); Esource_filt = (1-k)*E_Meas + k*Esource_filt;
% dividded by sum of voltages E_mean = Esource_filt; E_Ref_in = E_Ref_in /E_mean; E_mean = E_mean / N_mod;
%current polarity if(I_Meas > 0) I_pol = 1; else I_pol = -1; end
% Reference correction for individual references proportional = (E_mean-Esource_filt) * VArm_kp; integral = proportional * VArm_ki + integral; E_bal = proportional + integral;
% If control Enabled, reference update with corrected contributions
if(Balance_ENABLE) E_ref = E_Ref_in + I_pol * E_bal; else E_ref = E_Ref_in; end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % System update %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
sys(1) = E_ref; sys(2) = integral; sys(3) = Esource_filt;
sys = [sys(1) sys(2) sys(3)];
% end mdlUpdate % %============================================================================= % mdlOutputs % Return the block outputs. %============================================================================= % function sys=mdlOutputs(x) % ts sys = x(1);
% end mdlOutputs

Answers (0)

Asked:

on 20 Jul 2016

Community Treasure Hunt

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

Start Hunting!