0

folks!

I am trying to display the Simulink current simulation time. I have to notice that, in my case, the system is not viewable, once I use load_system, and it would be very useful to know how progress the simulation.

For that, I have read that I should use the function 'ssGetT'. To implement it, I am using S-function builder block and I succeeded. I mean, I was able to get the current simulation time.

However, I am caught at this point, because I do not know how display it either a progress bar or a message box or any other way. Important, display from an C environment in S-function builder.

If there is any other way to do it, please me. =)

If anybody could help me, I would really appreciate it.

Agaeme
  • 13
  • 1
  • 4
  • Why do you need it to be from a C-mex S-Function? If you're using load_system, then MATLAB must be available, and this would be almost trivial to do using an m-code S-Function. – Phil Goddard Jul 20 '14 at 18:42
  • @PhilGoddard , this is the problem! I load the system by 'load_system' and, then, I start the simulation by 'sim'. Once I have started it, any command in m-code just will be executed as soon as the simulation is finished. The same is happening in the s-function. I can get the time, and if I insert a display block, it results as a clock block, I mean, it works fine! However, I insert same command to print in the command view during the simulation and it is printed just after finishing simulation. – Agaeme Jul 21 '14 at 22:47

2 Answers2

3

A couple of things to note:

  1. There is no need to use load_system prior to using sim.

  2. As with any MATLAB command, sim blocks further execution of m-code after that line in your m-code (or the command line) until it has finished executing (which in this case means that the simulation has stopped).

But any m-code within the model will definitely get excuted during model execution.

For instance, create a model where you feed the Clock block into a MATLAB Function block. Within the MATLAB Function block have the following code

function fcn(t)
%#codegen
coder.extrinsic('fprintf');
persistent firstTime

if isempty(firstTime)
    firstTime = false;
    fprintf('Starting Now\n');
end

fprintf('time = %.4f\n',t);

This will print the simulation time, at every time step, to the MATLAB Command Window, while the simulation is running (irrespective of how the model is started).

Phil Goddard
  • 10,571
  • 1
  • 16
  • 28
  • Phil, thank you for your contribution. I have followed your example and it works perfect! I just have one question: there is any difference between use _s-function_ or _fcn_ block respect to increasing time simulation? – Agaeme Jul 23 '14 at 10:44
1

Updating...

To display a progress status in the commad view, I took Phil's suggestion.

I implemented this system in symulink in which the fcn inputs are the simulation time from a clock and the final simulation time.

I define SampleTime in the Digital Clock block as Final simulation time/steps, where steps is the number of time you want to update the progress. In my case, I update it at each 5% untill 100%, so steps is 20.

The fnc block is:

function fcn(t,tsim)
coder.extrinsic('fprintf');

persistent firstTime

if isempty(firstTime)
    firstTime = false;
    fprintf('\nSimulating...\n\n');
end

prog = 100*t/tsim;
fprintf(' %1.0f%%',prog);
Agaeme
  • 13
  • 1
  • 4