I couldn't able to run Level 2 M-file S function...

Below is my Level 2 M-file S function,
function svpwm_states(block)
function setup(block)
block.NumInputPorts = 2;
block.NumOutputPorts = 1;
block.SetPreCompInpPortInfoToDynamic;
block.SetPreCompOutPortInfoToDynamic;
block.InputPort(1).DirectFeedthrough = false;
block.InputPort(2).DirectFeedthrough = false;
block.SampleTimes = [-1 0];
block.SimStateCompliance = 'DefaultSimState';
block.RegBlockMethod('PostPropagationSetup', @DoPostPropSetup);
block.RegBlockMethod('Start', @Start);
%block.RegBlockMethod('InitializeConditions', @InitializeConditions);
block.RegBlockMethod('Outputs',@Output);
block.RegBlockMethod('Update', @Update);
function DoPostPropSetup(block)
block.NumDworks = 1;
block.Dwork(1).Name = 'k';
block.Dwork(1).Dimensions = 1;
block.Dwork(1).DatatypeID = 0; % double
block.Dwork(1).Complexity = 'Real'; % real
block.Dwork(1).UsedAsDiscState = true;
%block.Dwork(2).Name = 'x';
%block.Dwork(2).Dimensions = 1;
%block.Dwork(2).DatatypeID = 0; % double
%block.Dwork(2).Complexity = 'Real'; % real
%block.Dwork(2).UsedAsDiscState = true;
function Start(block)
block.Dwork(1).Data = 0;
function Output(block)
block.OutputPort(1).Data = block.Dwork(1).Data;
function Update(block)
vsd = block.InputPort(1).Data;
vsq = block.InputPort(2).Data;
k = block.Dwork(1).Data;
if vsd>0 & vsq>=0 & vsd>vsq;
k=k+1;
elseif vsd>0 & vsq>=0 & vsd<=vsq;
k=k+2;
elseif vsd<=0 & vsq>0;
k=k+3;
elseif vsd<0 & vsq<=0 & vsd<vsq;
k=k+4;
elseif vsd<0 & vsq<0 & vsd>=vsq;
k=k+5;
elseif vsd>=0 & vsq<0;
k=k+6;
end
block.Dwork(1).Data = k;
function Terminate(block)
but when I tried to RUN the m-file, it's not showing any error. It didn't run at all. Plus when I used this block in my simulink model it didn't create two I/P port as declare in the code. I'm using this S function for the first time...Where am I going wrong, can any one suggest it??

 Accepted Answer

You need to call setup from your top-level function:
function svpwm_states(block)
setup(block);

3 Comments

aah...silly me..Thank you Kaustubha...
I have corrected it now it runs But strangely it gives me this error ..
"Input argument "block" is undefined."
Try setting breakpoints in your code to see where the error occurs? You could also run "dbstop if all error" in MATLAB before running your model and see if the MATLAB debugger stops in your S-function. That should help you fix the error.

Sign in to comment.

More Answers (2)

I just wrote my first couple of these in the past few days, so take my suggestion with a grain of salt!!
I THINK you have to define your input and output's in more detail (where you currently just have the feed through marked as false).
I have these in my code, and it plays nice.
block.InputPort(1).Dimensions = 1; block.InputPort(1).DatatypeID = 0; % double block.InputPort(1).Complexity = 'Real'; block.InputPort(1).DirectFeedthrough = false;
Also - i THINK (big caveat there) that you'll want to remove the terminate function line, or call it up top after
block.RegBlockMethod('Update', @Update);
Hopefully this helps!!

1 Comment

I tried this but it won't help...atleast it should give me some error...it's not running at all...

Sign in to comment.

Categories

Find more on Modeling in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!