How do I automaticaly change values of blocks inside a reference model?

14 次查看(过去 30 天)
Hello everybody
I am using a reference model and including several instances of it inside of another model. Each instance has initial conditions, "To workspace" blocks, constants, etc., which need to be different among each instance. For example:
Instance 1-> Initial conditions: X_ini(1,1:3), "To workspace" name: X_out_1
Instance 2-> Initial conditions: X_ini(2,1:3), "To workspace" name: X_out_2
where X_ini [2,3] would be a matrix with all the initial conditions and X_out_1 and X_out_2 would be the variables saved in the Workspace.
One possibility would be to add an input and output port, and set the initial conditions and save the data from outside. The problem with this solution is that I do not have only initial conditions and saved variables, but many other things. I would be cleaner and faster if every time I introduce a reference model, it automatically changes those values.
Does anybody know how to do it? Should I use a library block instead?
Thank you very much in advance, Pablo

采纳的回答

Zack Peters
Zack Peters 2016-4-7
Hi Pablo,
What you are looking for is what we call the "Model Workspace". It allows you to pass in data that is scoped only to that particular model block - perfect for multi-instance model references.
Here is a link to get you started on reading about them: http://www.mathworks.com/help/simulink/ug/using-model-workspaces.html
At the bottom of that page are some links to examples on how to make this work.
~Zack
  1 个评论
Pablo García Auñón
编辑:Pablo García Auñón 2016-4-7
Hi again
Thank you very much for your answer!. I have tried using the Model Workspace as you proposed, without success.
First, I set a model argument to identify every instance of the model reference:
instance_number=1
In the 1st instance of the block model reference, I set instance_number=1, and y the 2nd instance_number=2.
Afterwards, I wrote a Model Callback function (InitFcn):
hws = get_param('multi_singleagent_1st0','modelworkspace');
instance_number=hws.evalin('instance_number');
hws.assignin('X_ini',X_ini_all(instance_number,:));
where X_ini is the vector with the initial conditions inside the Model Reference and X_ini_all is the matrix with all the initial conditions.
Well, I was expecting that for each instance, the proper initial condition vector X_ini_all(instance_number,:) was properly assigned to the vector inside that instace, X_ini. It actually assigns the predefined value instance_number=1 for both instances...that is, it is not doing what I meant.
Do can I do it? How do I identify each instance independently? Do I have to use a Model argument to identify them? I have only one common Model Workspace shared for both instances...
Thank you very much for your help!
Pablo

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Simulink Environment Customization 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by