Matlab Coder: dll with several external functions for singletone class

14 Ansichten (letzte 30 Tage)
Victor am 9 Mär. 2017
Kommentiert: Victor am 10 Apr. 2017
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?

Antworten (1)

Ryan Livingston
Ryan Livingston am 14 Mär. 2017
Bearbeitet: Ryan Livingston am 14 Mär. 2017
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;
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!

Translated by