[Simulink] Output only non-zero values
27 views (last 30 days)
Show older comments
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
Answers (3)
Sebastian Castro
on 9 Jun 2014
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
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.
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)=[];
0 Comments
See Also
Categories
Find more on Schedule Model Components in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!