How to use Global Objects in Matlab S-Function

3 vues (au cours des 30 derniers jours)
Diego
Diego le 4 Avr 2014
Hi! Im new with s-functions and I need to use an object among several functions on a Matlab S-Function. The object is used as a library and it is needed to set the conection with a server, get data from it in each loop and close it at the end of simulation. The use of the object its something like this:
ClassX ObjectX;
[Handle clientID]=ObjectX.setConnection(...);
while(coonection)
[result]=ObjectX.getPosition(Handle ClientID,...);
[result]=ObjectX.getAngle(Handle ClientID,...);
...
end
[result]=ObjectX.CloseConnection(...);
Its not convinient to instance and close the comunication in every loop. So I whant to create the object and set the conection on "function setup(block)", update data on "function Update(block)" and close the conection on "function Terminate(block)" but to do this I need to share the "ObjectX" an "clientID" among the functions.
It would also be useful to share the object instance among all the S-Function blocks used since it does not make sense to initiate a connection for each block.
I hope you can help me.

Réponses (1)

Kaustubha Govind
Kaustubha Govind le 4 Avr 2014
Modifié(e) : Kaustubha Govind le 4 Avr 2014
You could potentially search the model for all S-function Blocks of the same kind (you could perhaps create a mask over these blocks and set a MaskType to identify them), and see if any one of them already owns a UserData, and create a new instance if one doesn't already exist. (Note that this is not supported workflow - ideally, global data needs to be stored on Data Store Memory blocks)
You may also have the model be responsible for creating the object in the base workspace (via Model Callbacks), and have all your S-functions look there for the object.

Catégories

En savoir plus sur Model, Block, and Port Callbacks dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by