1
votes

I have a MATLAB function block in simulink and for each step simlulink does I want to input a counter with increment 1. Ex: 1st Step -> Acc=1 2nd Step -> Acc=2

I tried using a Count up block + Pulse generator but the time step of simulink is not constant.

Any ideas?

2
You need to add the code (and check formatting) that you have tried already. and also explain the results that you see (ie what's going wrong). - simo.3792
Simulink chooses a Variable Step Solver by default for you. If you want the steps to be equally distant in time, choose a Fixed Step Solver and specify the timestep instead, but know the implications of this in your model. If you want a block to be executed with a fixed periodicity, choose a Sample Time for it instead, or sample it with a Zero Order Hold, for example. - Manex
I'm not sure how you tried using the Pulse Generator, but isn't there a way to use the Pulse Generator so that the pulses are in sample hits rather than simulation time? Anyway, the method I used for counting hits is to use a C++ S-function, and then have a PWork vector that counts the calls. PWork is sort of a variable that stays persistent in memory. So, you append it at each function call and get the job done, regardless of solver used. - user2215426

2 Answers

0
votes

A common way to do this is to use a sum and a memory block with an initial condition of 0. It should count steps in both fixed and variable step simulations. In fact I believe this would be build and perform very much like an s-function solution during simulation.

0
votes

Why not just use an integrator block? You can choose with a discreet or continuous integrator block depending on your model type. You can specify the start conditions/value and reset conditions if needed. The image below shows an example of discreet and continuous blocks. Both are just using their default values. To do what you want (adding 1 to the output every step) just define the model sample time as an environment variable (eg sT=0.01) and set the integrator gain to be 1/sT.

Integrator example