Why does the memory allocation for handle class objects in a cell array cause an error in a loop?
Show older comments
I am unable to generate C++ code for an M-file that allocates memory for a handle class cell array during a loop using MATLAB R2021b.
The class that I am using is defined below. It has a property named "Value" that is assigned by an argument, or by the class function "setValue":
classdef BasicClass < handle %#codegen
properties
Value;
end
methods
function obj = BasicClass(val)
if nargin == 1
obj.Value = val;
end
end
function obj = setValue(obj, input)
obj.Value = input;
fprintf( 'set.Prop1 method called. Prop1 = %f\n', obj.Value );
end
end
end
Here is the relevant code that I am trying to generate code for:
Nodes = cell(1, Nobs);
for nobs = 1:Nobs
Nodes{nobs} = BasicClass(nobs);
end
tmp = Nodes{5};
"BasicClass" is a class that inherits handle, "Nobs" is a variable that I'm setting at my entry point (I want to set "Nobs" at the cli interface for my C++). I'm receiving this error when running my script to generate C++ code:
### Compiling function(s) my_entry_point ...
### Generating compilation report ...
??? Cannot allocate this handle object. For code
generation, a handle object allocated inside a loop
cannot be referenced outside of the loop.
How can I resolve this error?
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!