2

In simulink model I have a matlab function block. Inside the function I would like to create an array of objects in a way it is compatible with code generation.

My question is similar to the one answered here: Construct an array of objects in MATLAB

The problem is "compatible with code generation" part.

When I try to do it with repmatmatlab returns:

Arrays of objects are not supported for code generation.

When I try to do it with array of objects I see:

Recursive calls are not allowed. Function 'dummyClass.dummyClass' participated in a recursive call.

Please find below the code I run:

embedded matlab function

function y = fcn(u)
%#codegen
x = [1 2 3];
% %% repmat way
% aa = dummyClass(x(1));
% aaArray = repmat(aa,1,3);   
%% array of objects
aa = dummyClass(x);

y = u;

class file

classdef dummyClass    
    properties
        value
    end

    methods
        function obj = dummyClass(value)
           %% array of objects
            if nargin~=0
                m = size(value,1);
                n = size(value,2);
                obj(m,n) = dummyClass;
                for i = 1:m
                   for j = 1:n
                      obj(a,b).value = value(a,b);
                   end
                end
            end
% %% repmat
%             obj.value = value;
        end
    end

end

Uncomment

Community
  • 1
  • 1
Kape
  • 195
  • 1
  • 8

1 Answers1

1

As of MATLAB R2017a, there's no way to create arrays of objects that is compatible with code generation using MATLAB Coder or Simulink Coder.

As the first error message says, "arrays of objects are not supported for code generation" - it's not a problem with any specific way you're attempting to create them, they're just not supported at all.

MathWorks may introduce this feature in a future version, but it's not there right now.

Sam Roberts
  • 23,951
  • 1
  • 40
  • 64