Matlab Coder: dll with several external functions for singletone class
조회 수: 4 (최근 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!