Question about S function

Hi,
Is it possible to realize the following with Level 2 S function?
The input is a binary data, that is, every time only one bit can be read. The output shall save up to 10 bit and give them out. In this case the sample times at the input and at the output are different.
The problem is, in S function "block.InputPort(1).Data" is only valid for the current sample time.
Thanks Senmeis

 采纳的回答

Kaustubha Govind
Kaustubha Govind 2012-10-10

0 个投票

You need to create DWork vectors for the S-function to store all previous inputs.

更多回答(1 个)

Thank you.
With your advice I write the following code:
function DoPostPropSetup(block)
block.Dwork(1).Dimensions = 10;
function Update(block)
number = 1;
if number <= Block.Dwork(1).Dimensions
block.Dwork(1).Data(number) = block.InputPort(1).Data;
number = number + 1;
end;
function Outputs(block)
block.OutputPort(1).Data(1:block.Dwork(1).Dimensions) = block.Dwork(1).Data;
But it seems that only the first element of “Block.OutputPort(1).Data” is assigned. That is, “Block.OutputPort(1).Data” doesn’t wait for 10 elements before outputting. Do I need a flag to signal the status as used in C?
Thanks Senmeis

3 个评论

Owen: The line "number=1" basically means that every time the Update method runs, number is assigned to 1 - even though you increment it towards the end of the function, that variable goes out of scope at the end of the Update function. I would recommend that you create another DWork to store the "number" value, so the value doesn't go out of scope by the next iteration.
I tried the following code:
function DoPostPropSetup(block)
block.Dwork(2).Dimensions = 1;
block.Dwork(2).DatatypeID = 0;
block.Dwork(2).Complexity = Real;
function Start(block)
block.Dwork(1).Data = zero(block.Dwork(1).Dimensions, 1);
block.Dwork(2).Data = 1;
function Update(block)
if block.Dwork(2).Data <= block.Dwork(1).Dimensions
block.Dwork(1).Data(block.Dwork(2).Data) = block.InputPort (1).Data;
block.Dwork(2).Data = block.Dwork(2).Data + 1;
else
block.Dwork(2).Data =1 ;
end;
The idea is, data of different samples is stored in Block.Dwork(1).Data and number of samples is stored in Block.Dwork(2).Data, but I encountered an error:
“Subscript indices must either be real positive integers or logicals.”
for this line:
block.Dwork(1).Data(block.Dwork(2).Data) = block.InputPort(1).Data;
I also tried the following variants but it doesn’t help.
block.Dwork(2).DatatypeID = 1;
block.Dwork(2).DatatypeID = 2;
block.Dwork(2).DatatypeID = 3;
block.Dwork(2).DatatypeID = 4;
block.Dwork(2).DatatypeID = 5;
What is wrong with it?
Thanks Senmeis
Owen: You cannot change the size of DWork vectors at runtime. It is best to initialize it to a vector of 10 zeros and gradually fill it in.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Schedule Model Components 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by