3

I am looking to set up a test set for an existing Simulink model. Ideally I could take full control of the model, explicitly stepping it and measuring the state of any signal on any bus in the model.

As might have been gleaned, this is the precursor of a unit testing system for the model. Being so, I can't really justify changing the model to suit the test, the test must accommodate the model as-is.

The furthest I've got so far is using load_model() to return a handle to the model. From there there seems to be a quite obscure set of functions for accessing the model. I can't see any that relate to accessing states and can't see any further commands that relate to accessing a loaded model.

J Collins
  • 2,106
  • 1
  • 23
  • 30
  • I could really need an reduced example of your model, and what values you actually want to obtain. You might also have a look at the `Simulink preferences` where you have the `Data Import/Export` tab. You can check `states` there and you get a `xout` variable with your states. Maybe that already answers your question. – Robert Seifert Oct 14 '13 at 09:11
  • @thewaywewalk that sounds almost perfect save that it isn't outputting anything. I've checked the box for 'States' and allowed it to create the 'xout' variable in the workspace. Now with the model paused, tahe variable doesn't exist. What am I missing? – J Collins Oct 14 '13 at 09:26
  • The only thing I could imagine is, that you also checked the box 'Save simulation output as a single object'. Then you would need to access the `out` variable. If this is unchecked, it saves every state after every time step. – Robert Seifert Oct 14 '13 at 09:43
  • Unfortunately it never did in this model. I did create a generic model and prove that it worked, just something wrong in the model I guess. Someday I'll come back to the project. :/ – J Collins Oct 25 '13 at 16:32

2 Answers2

3

The easiest way is to use the Data Import/Export function within the Simulink Preferences.

Set the checkbox States and it will store every state of your system for every time step in your workspace, also when you pause the simulation or execute it step by step.

Be aware not to set Save simulation output as single object, in this case the access would be more complicated and you need to follow the instructions here.

Robert Seifert
  • 25,078
  • 11
  • 68
  • 113
  • To continue the discussion, I still can't see why my model isn't saving anything, perhaps some optimisation or other is going on that is getting in the way. I have created a new generic model that is giving the expected output, but the very next thing I tried was to 'log signal data' on some signals in the simple model. I'm back to having logging output nothing at all. – J Collins Oct 14 '13 at 10:28
  • But do you have the objects in your workspace and they are just "empty" or do you not have objects at all? Is it possible that your model does not contain elements which have "states"? In this case signal logging is the thing you need. Their evaluation is more complicated and you could have a look at my answer [here](http://stackoverflow.com/questions/19176680/how-can-i-get-signal-dimensions-in-simulink-model/19177681#19177681) – Robert Seifert Oct 14 '13 at 10:35
  • @JCollins Your model isn't saving anything because you are pausing it. The data is written to the workspace when the execution of the model is finished, not when it's paused. – am304 Oct 14 '13 at 11:48
  • @am304 but states are also saved when it's paused or for step-by-step execution. – Robert Seifert Oct 14 '13 at 11:55
0

To add to the other answer, you probably want to check this page in the documentation: Control Simulation Using the set_param Command. Of interest are the following commands:

set_param(<model_name>, 'SimulationCommand', 'start')
set_param(<model_name>, 'SimulationCommand', 'pause')
set_param(<model_name>, 'SimulationCommand', 'WriteDataLogs')
set_param(<model_name>, 'SimulationCommand', 'continue')

Replace <model_name> by the path to your model file.

am304
  • 13,758
  • 2
  • 22
  • 40