Matlab Coder: dll with several external functions for singletone class
3 次查看(过去 30 天)
显示 更早的评论
I'm trying to generate dll library for a custom Matlab class, that can have its internal states and several external functions (e.g. addData(), getData(), getState1(), getState2(), etc.). And these functions should be called from an external application to communicate with dll.
In Matlab Documentation there is a topic: Generate Fixed-Point MATLAB Code for Multiple Entry-Point Functions , and it says: "generating a single C/C++ library for more than one entry-point MATLAB® function allows you to communicate between library functions using shared mem: ry".
In these terms, I need two or more entry-point functions that work with a single class instance. (Or share the same memory, since classes in Matlab are converted to structures in C.)
But I can't define a global/persistent class variable inside several entry points, nor generate code directly for Matlab class. How should I define these external functions to work with the same data?
0 个评论
回答(1 个)
Ryan Livingston
2017-3-14
编辑:Ryan Livingston
2017-3-14
This assumes your class is a handle class so you can access the same instance.
You can use a persistent variable in a getSetInstance function/method:
function o = getSetInstance(obj)
persistent p;
if isempty(p) || nargin > 0
p = obj;
end
o = p;
Then you can call getSetInstance(MyClass(...)) once to allocate your persistent variable. Any calls:
obj = getSetInstance();
will give you access to that single object instance.
This is just a way to implement the singleton pattern in MATLAB. You can find other ways to implement this by searching like:
You could also imagine wrapping this up in a static method in your class.
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!