Is it possible to access referenced model arguments from InitFcn callback?
Show older comments
I have a Simulink model with multiple instances of a referenced model which I am passing the argument 'vehicle' to in order to configure each model instance appropriately. One parameter I need to modify in the model is the port number of a TCP Receive block. As this block can not accept variables as arguments (e.g. 8000 + vehicle), I am attempting to use the models InitFcn to set the port parameter with the following code
mws = get_param(bdroot,'ModelWorkspace')
if (~mws.hasVariable('vehicle'))
error('Unkonwn variable vehicle');
end
veh = mws.getVariable('vehicle')
recv_port = num2str(8000 + veh - 1)
set_param([bdroot '/Receive'],'Port',recv_port);
Obviously, if I update this model in isolation I would expect the 'veh' variable to be empty as the argument has not been set. But when I update the master model I would expect the parameter to have its value set, but this is not the case, it always returns zero.
I have attached a cut down example which just uses two constant blocks to illustrate the problem, the first of which is updated by the InitFcn and always ends up as 8000, whereas the second is set correctly.
Answers (1)
Sebastian Castro
on 4 Feb 2015
0 votes
Owen,
That was a tough one!
I think I figured out a way to do it using masks instead of the InitFcn callback. What I did was mask the Constant block with the port, and added the set_param code you had to the initialization commands of the Mask Editor. I hope the picture below helps.

I've also attached the models in R2014b format, so you can use them as you need. My output looks as follows, which is hopefully what you expected :)

- Sebastian
2 Comments
Owen
on 5 Feb 2015
Sebastian Castro
on 5 Feb 2015
If you use a masked library block instead of model reference, does this work? It may not be ideal, but at least could prevent you from having to write your own S-Function.
Note: If you really want model reference for the rest of the blocks that process the received message, you can just pull the TCP block out of the model and feed it into the input port of your new referenced model.
Categories
Find more on Modeling in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!