Question about S function

조회 수: 5 (최근 30일)
Owen
Owen 2012년 10월 10일
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일
You need to create DWork vectors for the S-function to store all previous inputs.

추가 답변 (1개)

Owen
Owen 2012년 10월 11일
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
Owen 2012년 10월 13일
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
Kaustubha Govind
Kaustubha Govind 2012년 10월 16일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile 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