Matlab Coder: dll with several external functions for singletone class

4 visualizaciones (últimos 30 días)
Victor
Victor el 9 de Mzo. de 2017
Comentada: Victor el 10 de Abr. de 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?

Respuestas (1)

Ryan Livingston
Ryan Livingston el 14 de Mzo. de 2017
Editada: Ryan Livingston el 14 de Mzo. de 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;
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.

Categorías

Más información sobre MATLAB Coder en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by