What is the best way to work with variable size class properties?
Show older comments
Hi,
I would like to generate C++ code for a object oriented matlab algorithm that fully exploits dynamic memory allocation.
Right now my code contains data objects (see obj_data_fixedsize10000) that include a property 'data' with fixed size. For the code generation I'm using the following configuration:
cfgexe = coder.config('exe');
cfgexe.DynamicMemoryAllocation = 'Off';
The memory that is allocated after code generation is huge, so this approach won't work for my project.
Instead I'd like to work with variable size arrays. Unfortunately, this feature is currently explicity not supported for class properties ('coder.varsize' is not supported for class properties, see [1]).
I figured out a way to use a persistent variable 'data_pers' (see obj_data_persistent below) that can be declared as variable size to accomplish my needs. However, the object obj_data_persistent can be instanced only once since a second instance would use the same persistent variable.
Besides, I assume that it could also be kind of tricky to pass an instance of obj_data_persistent as an input to an entry point function.
Therefore my question: Are there any more elegant procedures to work with objects that contain variable size variables?
Thanks
Johannes
classdef obj_data_fixedsize10000
properties (Access = private)
data = zeros(10000,1);
n_valid = 1;
end
properties (Constant = true)
n_max = 10000;
end
methods
function val = getData(obj)
val = obj.data(1:obj.n_valid);
end
function obj = setData(obj,val)
obj.n_valid = min([length(val) obj.n_max]);
obj.data(1:obj.n_valid) = val(1:obj.n_valid);
end
end
end
classdef obj_data_persistent
methods
function val = getData(obj)
val = obj.persData;
end
function obj = setData(obj,val)
obj.persData(val);
end
end
methods (Access = private)
% Handling Persistent Data
function varargout = persData(~,varargin)
persistent data_pers
if isempty(data_pers)
data_pers = 0;
end
coder.varsize('data_pers',[inf 1],[1 0]);
if nargin == 2
data_pers = varargin{1};
end
if nargout == 1
varargout{1} = data_pers;
end
end
end
end
Accepted Answer
More Answers (0)
Categories
Find more on Algorithm Design Basics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!