How to use Global Objects in Matlab S-Function
1 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(1 个)
Kaustubha Govind
2014-4-4
编辑:Kaustubha Govind
2014-4-4
Please see if my answer here helps: http://www.mathworks.com/matlabcentral/answers/27902-working-with-matlab-classes-in-the-level-2-matlab-s-functions
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Model, Block, and Port Callbacks 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!