How do I access simulation end time of Simulink model from within a MATLAB function block?

I'm writing a MATLAB function block which would like to know how long the simulation it contains is to be run. I.e. I would like to set a variable in my MATLAB function block to be equal to whatever number the user chose as simulation end time (10 by default).
I would guess / hope that this is a simply accessible property of some object or another, but I cannot seem to find out what is it called or where to read it off.
Ideally I would like to include this block in a library to be used in different models, so it shouldn't need to have any prior information about the model it is used in.

 Accepted Answer

I think the easiest thing to do is define a variable in MATLAB base workspace -- let's call it tEnd.
Then, set your simulation end time to tEnd. On the other hand, add the variable tEnd to your MATLAB Function block as a parameter, as shown in this page.
- Sebastian

1 Comment

Thank you very much, that's a very clever solution.
It is not quite ideal for my purposes of writing a block for a library whose users should (if possible) not be burdened by compliance to the standard's the block's programmer set (i.e. defining their endtime as a workspace variable). But for most other purposes (particularly programming for yourself), this has to be the quickest and best way to do it.

Sign in to comment.

More Answers (1)

In the meantime, I did find a way to do it. In the function block that wants to know the simulation end time, call the following code:
current_system = get_param(0, 'CurrentSystem');
% Now current_system refers to the block I'm calling from.
parent_system = get_param(current_system, 'Parent');
% Now parent_system refers to the model its in.
stop_time_as_str = get_param(current_system, 'StopTime');
% Now stop_time_as_str is something like '10.0'.
stop_time = str2double(stop_time_as_str);
% This is the stop time as numeric.
If the block is buried under several layers of subsystems, the parent calling needs to be iterated, e.g. with a while loop running for as long as the parents parameter is not empty.

4 Comments

Yes, I didn't want to suggest that because it requires you to label get_param and str2double as extrinsic using coder.extrinsic.
This means that your function will work only in simulation; if you generate code, the code will be looking for an extrinsic definition of these 2 functions that you would need to provide.
If this block is staying in simulation world, that is completely fine.
NOTE: By the way, you could just use the bdroot function to immediately find the name of the top model ;) ... which you will also have to label as extrinsic.
- Sebastian
Indeed, I am planning to stay in simulation world without extracting code, so I'm free to use all manner of nasty extrinsics.
Thanks for the tip with the bdroot function by the way, much more elegant than what I did by hand with my loops!
I also have the same problem, what code is that, matlab or C? cuz I put it in the Matlab function block but arose error 'Function 'get_param' not supported for code generation.'
Assuming you don't actually need to use this function for code generation (perhaps just for testing), you can bypass the code generation for that function using "coder.extrinsic('get_param');"

Sign in to comment.

Categories

Products

Community Treasure Hunt

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

Start Hunting!