[Simulink] Output only non-zero values

조회 수: 56 (최근 30일)
Georgiy
Georgiy 2014년 6월 9일
댓글: TAB 2018년 6월 1일
Hello, How can I setup MATLAB function block so it outputs only a non-zero values? I have some code in this block that finds peaks and peaks indexes of the input signal (please don't suggest other methods for peak finding, I need to use this particular code). The problem is that for each iteration, block outputs something - if it finds the peak in current iteration, it outputs its height and index into MATLAB in [index,peak height] format, if it doesn't find the peak - it outputs [0,0]. So after all I get in MATLAB a final matrix, which looks like:
0,0
0,0
...
...
first_peak_index, first_peak_height
0,0
0,0
...
...
second_peak_index, second_peak_height
0,0
0,0
...
and so on. How I force the MATLAB function block to output only a non-zero values? So overall I will get the following matrix in MATLAB:
first_peak_index, first_peak_height
second_peak_index, second_peak_height
third_peak_index, third_peak_height
and so on.
Or, alternatively, what block (filter?) can I put after the MATLAB function block that will pass only a non-zero values?
Thank you!

답변 (3개)

Sebastian Castro
Sebastian Castro 2014년 6월 9일
You could try putting an Enabled Subsystem after the output of the MATLAB Function block.
You can use the condition that "output is not equal to zero" to drive your system. If this is true, pass the output straight through; otherwise, do not output anything.
The following screenshot shows what this could look like:
  댓글 수: 2
HINA FATHIMA
HINA FATHIMA 2018년 5월 31일
Simple n brilliant
TAB
TAB 2018년 6월 1일
Enabled Subsystem wont work as expected in the question. Enabled Subsystem generates output (based on Held/Reset settings) even when it is disabled.

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


Georgiy
Georgiy 2014년 6월 10일
Hi Sebastian, Thank you for trying to help, but it still doesn't work. My input signal is a 20000 descrete vector. Not all the vector is processed at the time, but each value of it separately. I tried to put an Enabled Subsystem, as you suggested, but still - my output to the MATLAB is a 20000 vector, no matter what. I tried to put different conditions (~=, >, =<, =) in a comparator - output vector is always 20000. If I put "<" in a comparator, for example, I get output vector that consists of [0,0] values only. If I put "~=", output vector is:
0,0
0,0
...
...
first_peak_index, first_peak_height
0,0
0,0
...
...
second_peak_index, second_peak_height
0,0
0,0
...
Best regards.

TAB
TAB 2018년 6월 1일
You can not stop Simulink to generate the output at any time step.
In simple words, at every time step, Simulink works as below:
Sample the Inputs > Process > Generate outputs
Enabled Subsystem also won't work becuase when it is disabled, then also its generates output. In disabled condition its output is one of the below depend on your settings:
> Previous value (Held)
> Initial value (Reset)
To meet your requirement, you can post-process the generated output using m-script. Make m-script or function to delete the non-zero values from output.
outputs(outputs==0)=[];
If you want to do post processing automatocally after simulation, use StopFcn Callback of model.

카테고리

Help CenterFile Exchange에서 Sources에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by