How to use Global Objects in Matlab S-Function

1 回表示 (過去 30 日間)
Diego
Diego 2014 年 4 月 4 日
編集済み: Kaustubha Govind 2014 年 4 月 4 日
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.

回答 (1 件)

Kaustubha Govind
Kaustubha Govind 2014 年 4 月 4 日
編集済み: Kaustubha Govind 2014 年 4 月 4 日
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.

カテゴリ

Help Center および File ExchangeSimulink Functions についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by