Vector input for MATLAB function block

8 views (last 30 days)
Hello everyone,
I've been struggling with this problem for a while now and it's beginning to drive me mad:
I have a Simulink MATLAB function block that is supposed to perform some calculations with the last 100 values of a signal. Is there a way to dynamically store the last 100 values of said signal and feed it as an input into the MATLAB function block without using 100 inputs and thousands of memory blocks?
Best regards,
Daniel
  1 Comment
Daniel Schmidt
Daniel Schmidt on 27 Mar 2018
I think I found a solution for my problem now. Basically, I introduce a second MATLAB function block to create and store the values.
Since I found this question a couple of times online, I uploaded an example for the calculation of a moving average using this method.
The disadvantage of my solution is, that the vector size (or time constant) needs to be defined directly inside the fcn block but that's fine for my purpose.
Of course, I'm always open for other suggestions. Maybe someone has a more elegant solution?!

Sign in to comment.

Accepted Answer

Birdman
Birdman on 27 Mar 2018
Well, although your problem is ok, usage of Memory block always increases the chance for system to fail if the complexity of your system increases. The best way to hold the value of a signal is to use Enabled Subsystem and that is what I did. I also kept your way and made a comparison. My way also works, see if that helps to you. Notice how I used the Pulse Generator. I also changed the solver type to variable-step since I wanted both your way and my way to take the same step size while solving.
  7 Comments
Birdman
Birdman on 28 Mar 2018

I changed the Memory block with Enabled Subsystem block this time only. Run it and see the results.

Daniel Schmidt
Daniel Schmidt on 28 Mar 2018
Thank you for the new model. Everything works as intended. Btw, with the delay block, your model even works without the enabled subsystem.
I am going to mark my question as solved. Have a nice day and thanks again.

Sign in to comment.

More Answers (0)

Categories

Find more on Schedule Model Components in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!