cell() constructor is not supported for code generation

2.3k views Asked by At

When I use matlab function block in Simulink. I use another matlab function which contains cell type. But simulink reported error message because of cell. How to avoid this? I'm kind of don't want modify the function because all my functions have cell it's a lot of work to modify them all. Error message: Simulation 28 Clear Save 11:26:28 PM Nov 27, 2014 Elapsed: 7 sec

cell() constructor is not supported for code generation

Function 'RealCo.m' (#56.102.111), line 5, column 4: "cell(9,1)" Launch diagnostic report. Component: MATLAB Function | Category: Coder error

My function:

function Co=RealCo(q)
l=60;
%%%Coordinate Calculation
%Guess value adopt the unique position below
Co=cell(9,1);
for i=1
    Co{i}=[0;0;0];
end

Co{1}=[sqrt(3)*l/4;0;0];
Co{2}(1)=-sqrt(3)*l/4;
Co{2}(2)=l/2;
Co{2}(3)=0;
Co{3}(1)=Co{2}(1);
Co{3}(2)=-l/2;
Co{3}(3)=0;

Co{4}=[sqrt(3)*l/4;-l/2;sqrt(3)*l/2];
Co{5}=Co{4};
Co{5}(2)=-Co{5}(2);
Co{6}=Co{4};
Co{6}(1)=-Co{6}(1);
Co{6}(2)=0;

for i=7:9
    Co{i}=Co{i-6};
    Co{i}(3)=sqrt(3)*l;
end

guess=zeros(27,1);
for i=1:3:27
    %When i=1,j=1 i=4,j=2 i=7,j=3 ...
    j=(i-rem(i,3))/3+1;
    guess(i)=Co{j}(1);
    guess(i+1)=Co{j}(2);
    guess(i+2)=Co{j}(3);
end

%Use fsolve to solve the system based on current value of q
fC=@(R)Coor(R,q);
[result, fval, exit, output]=fsolve(fC,guess);
% result
% fval
% eqns(guess)
% output
for i=1:3:27
    %When i=1,j=1 i=4,j=2 i=7,j=3 ...
    j=(i-rem(i,3))/3+1;
    Co{j}=[result(i);result(i+1);result(i+2)];
end
1

There are 1 answers

0
hbaderts On BEST ANSWER

Please read through the MATLAB Code Generation Manual. There, in the General Limitations section, it clearly states that

You cannot use cell arrays in your code.

The solution to this is presented in the MATLAB Central: You have to replace the cell arrays by e.g. structs. As I see from your code, each entry of your variable Co is either a 1x3 or a 3x1 array. It should therefore easily be possible to replace Co by a 9x3 matrix.

Of course it is quite some work, but these are just limitations of the MATLAB coder.