0

I want to use a timer to read the data from a simulink block to the workspace during simulation. I made a simple mdl model composed of a clock connected to a scope. Then I wrote this simple code:

t=timer('period', 1, 'taskstoexecute', 10, 'executionmode', 'fixedrate');
t.Timerfcn={@TimeStep}; 
start(t)

function time = TimeStep (~,~)
load_system('mymodel');
set_param('mymodel','SimulationCommand','start');
block='mymodel/Clock';
rto=get_param(block,'runtimeObject');
time=rto.OutputPort(1).Data;
disp(time);

The problem is that when I run the code for simulation time 10, it shows me "0" in work space and repeat it ten times. I assume that it should show me the time from 1 to 10. I have also modifies the solver to a discrete solver with time step=1. The other thing I do not understand is that when I put a ramp function instead of the clock and change it to: block='mymodel/Ramp';'

then I receive an error of "too many inputs". I would appreciate any help.

Phil Goddard
  • 10,571
  • 1
  • 16
  • 28

1 Answers1

0

You have two things that count time and seem to think that one of them is controlling the time in the other. It isn't.

More specifically, you have

  1. A 'Timer' in MATLAB that you have asked to run a certain piece of code once per second over 10 seconds. (Both times are measured in wall clock time.)

  2. Some MATLAB code that loads a Simulink model (if it isn't already loaded); starts the model (if it isn't already started); and gets the value on the output of a Clock block that is in the model (it does this only once each time the code is executed). As with every Simulink model, it will execute as fast as it can until the simulation end time is reached (or something else stops it).

So, in your case, each time the Timer executes, the simulation is started, the value at the output of the clock is obtained/printed (which since it happens very quickly after the start of the model it prints out that the simulation time is 0); and then (because you have a very simple simulation that takes no time at all to finish) the simulation terminates.

The above happens 10 times, each time printing the value of the clock at the start of the simulation, i.e. 0.

To see other values you need to make your simulation run longer - taking at least 1 second of wall clock time. For instance, if you change the solver to fixed step and put in a very small step size, something like 0.000001, then the simulation will probably take several seconds (of wall clock time) to execute.

Now you should see the Timer print different times as sometimes the model will still be executing when the code is called (1 second of wall clock time later).

But fundamentally you need to understand that the Timer is not controlling, and is not dependent upon, the simulation time, and vice-versa.

(I'm not sure about the issue with using Ramp but suspect it's got to with Clock being a fundamental block while Ramp is a masked subsystem.)

Phil Goddard
  • 10,571
  • 1
  • 16
  • 28
  • Thank you for clarification. I asked this question to find a solution for my previous question: https://stackoverflow.com/questions/52912158/simulink-saves-data-in-excel-during-simulation. Could u pls have a look at it. I would be so thank full. – user1907354 Oct 25 '18 at 04:54