In R2018b MATLAB Coder - autocode array of strings in a class properties (SetAccess = private)

16 views (last 30 days)
Katherine Goossen
Katherine Goossen on 4 Jun 2019
Answered: Denis Gurchenkov on 4 Jun 2019
I have created a class in which I am able to autocode everthing except the list of strings I have just added to the system. Right now I have defined the class similar to the following:
classdef MyClass < handle
properties (SetAccess = private)
% Internal data structure
idCnt = uint64(0);
stringType = coder.typeof('string', [1,Cnst.MaxNum]);
numItems = zeros(1,1,'uint32');
id = zeros(Cnst.MaxNum,1,'uint64');
...
other properties
...
end % properties private
function [myValues] = myFunction(T, MyInputs)
for k = 1:int32(T.numItems)
MyNewString(k).stringType = T.stringType(k);
...
end
When I attempt to use the coder to auto-code, I get the following error:
Code generation only supports initial values of class type for constant properties. Property 'stringType' of class 'MyClass' is not constant.
Can you help me with this so I can get this in a state where it will auto-code?

Answers (1)

Denis Gurchenkov
Denis Gurchenkov on 4 Jun 2019
Hi Katherine, if this class is intended to be used by the MATLAB coder that is converted to C code (via MATLAB Coder), then there are several things you might need to change:
  • Do not use coder.typeof. Coder.typeof is intended to be used to create input types that are given to MATLAB Coder command-line tool (codegen) as inputs. Coder.typeof objects are not intended to be used inside the code that is translated by coder.
  • As you found out, coder does not support properties whose initial values are classes. So instead of definiing the initial value of stringType, assign that property in the constructor of the class.
  • MATLAB Coder does not support arrays of strings. It supports scalar strings, or arrays of characters. Those are two alternatives.
  • MATLAB Coder does not support array growth through indexing. So this line:
MyNewString(k).stringType = T.stringType(k);
is not going to compile. You need to create MyNewString array prior to the loop.
hth,
Denis

Community Treasure Hunt

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

Start Hunting!

Translated by