This question is closed.

Asked by Mohamed Chaouechi
on 18 Oct 2017

function [sys,x0,str,ts] = SimuKalmanFilter(t,x,u,flag)

switch flag

case 0

[sys, x0, str, ts]=mdlInitializeSizes;

case 1

sys=mdlDerivatives(t, x, u);

case 2

sys=mdlUpdate(t, x, u);

case 3

sys=mdlOutputs(t, x, u);

case 4

sys=mdlGetTimeOfNextVarHit(t, x, u);

case 9

sys=mdlTerminate(t, x, u);

otherwise

error(['Unhandled flag=',num2str(flag)]);

end

function [sys, x0, str, ts]=mdlInitializeSizes

sizes=simsizes;

sizes.NumContStates=0;

sizes.NumDiscStates=1;

sizes.NumOutputs=1;

sizes.NumInputs=1;

sizes.DirFeedthrough=1;

sizes.NumSampleTimes=1;

sys=simsizes(sizes);

x0=5000+sqrt(5)*randn;

str=[];

ts=[-1 0];

global P;

P=5;

function sys=mdlDerivatives(t, x, u)

sys=[];

function sys=mdlUpdate(t, x, u)

global P;

F=1;

B=0;

H=1;

Q=0;

R=5;

xpre=F*x+B*u;

Ppre=F*P*F'+Q;

K=Ppre*H'*inv(H*Ppre*H'+R);

e=u-H*xpre;

xnew=xpre+K*e;

P=(eye(1)-K*H)*Ppre;

sys=xnew;

function sys=mdlOutputs(t, x, u)

sys=x;

function sys=mdlGetTimeOfNextVarHit(t, x, u)

sampleTime=1;

sys=t+sampleTime

function sys=mdlTerminate(t, x, u)

sys=[];

if true

% code

end

Answer by Robert Henson
on 18 Oct 2017

Are you running the function by pressing the "Run" button? If so, as this function requires inputs you will have to override the default behavior of the Run command. The Run Functions in the Editor section of the MATLAB Programming Guidedescribes how to do this.

Alternatively you can use the Command Window. Assuming that you have loaded variables t,x,u, and flag into your workspace, then in the Command Window type:

[sys,x0,str,ts] = SimuKalmanFilter(t,x,u,flag)

Pressing the Run button without modifying the default behavior is equivalent to typing

SimuKalmanFilter

in the Command Window. That is, none of the inputs are passed to the function.

Mohamed Chaouechi
on 19 Oct 2017

Robert Henson
on 20 Oct 2017

If x,t,u and flag are not in the workspace then you will continue to get the error. I'm still not quite sure how you intend to use this but looking at your function it looks like when flag is 0 you generate some data, then in further iterations when flag is another value you do something with this data. If that is the case you could add this to the top of the file

if nargin == 0

flag = 0;

end

That will solve the "Not enough input" problem but that may just push the issue to the case when flag=1.

The documentation has plenty of examples to help you get familiar with how functions are called from the Command Window, from the Editor, and from within Simulink. This may be a good place to start: https://www.mathworks.com/help/simulink/sfg/s-function-examples.html

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Nicolas Schmit (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/361888-i-still-have-problems-after-this-code-not-enough-input-arguments-error-in-switch-flag-anyone-ple#comment_494152

## Mohamed Chaouechi (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/361888-i-still-have-problems-after-this-code-not-enough-input-arguments-error-in-switch-flag-anyone-ple#comment_494566