0

What am I trying to do is to save in a variable (global or constant) the system time. I am using a S-function in Simulink. The problem is that when I store the value of the system time in a variable it is continuously incrementing so when I do the difference between the current system time and the time stored in my variable is always 0. What do you think is the solution storing the system time in a variable and what type of variable should I use a global one or a constant. If you have any answer please give me an example because I am new in Matlab. P.S I am using C language for the S-function.

Phil Goddard
  • 10,571
  • 1
  • 16
  • 28
  • Global variables are slow and should be avoided – patrik Mar 31 '14 at 08:34
  • How do you read the system time in C: with C functions (time.h) or as a function parameter from Simulink? – remus Mar 31 '14 at 09:17
  • Can you post some sample code and in which callback you are trying to store and do your calculations in s-function? – Navan Mar 31 '14 at 13:12
  • @patrik First of all I want to see if it is working and after I'll find for another solution which should be faster. – Alexandru Tat Apr 01 '14 at 07:11
  • @remus Currently I am using the clock block as an input for the S-Function, but from how I know I can always use ssGetT(S) function, what do you suggest? – Alexandru Tat Apr 01 '14 at 07:14

1 Answers1

1

It sounds as if you are trying to store the system time at the start of the simulation, then during the simulation compare the system time to that stored value. If so, then you should be using anR-Work vector to store the initial system time.

So in mdlInitializeSizes you want

ssSetNumRWork(S, 1);

Then in mdlStart you want

real_T *P_Tinit=ssGetRWork(S);

P_Tinit[0]=((real_T) clock())/CLOCKS_PER_SEC;

Then when you want to use the value use,

real_T itime;
itime=ssGetRWorkValue(S,0);

(The above assumes that you know how to actually get the system time, i.e. include the correct libraries, which from your question it sounds as if you do.)

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