MATLAB Answers

Georgiy
0

[Simulink] Output only non-zero values

Asked by Georgiy
on 9 Jun 2014
Latest activity Commented on by TAB
on 1 Jun 2018
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!

  0 Comments

Sign in to comment.

3 Answers

Answer by Sebastian Castro on 9 Jun 2014

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 Comments

Simple n brilliant
TAB
on 1 Jun 2018
Enabled Subsystem wont work as expected in the question. Enabled Subsystem generates output (based on Held/Reset settings) even when it is disabled.

Sign in to comment.


Answer by Georgiy
on 10 Jun 2014

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.

  0 Comments

Sign in to comment.


Answer by TAB
on 1 Jun 2018

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.

  0 Comments

Sign in to comment.