MATLAB to C-code

2.7k views Asked by At

I am following MathWorks guide to converting MATLAB code to C-code. The first step is to enter

%#codegen

after every function that I want converted to C-code, however doing so has given me the following prompt on the code below.

function lanes=find_lanes(B,h, stats)
% Find the regions that look like lanes
%#codegen

lanes = {};
l=0;
    for k = 1:length(B)
    metric = stats(k).MajorAxisLength/stats(k).MinorAxisLength;
    %testlane(k);
    %end
    %function testlane(k)
        coder.inline('never');
        if metric > 5 & all(B{k}(:,1)>100)
            l=l+1;
            lanes(l,:)=B(k);
        else
            delete(h(k))
        end
    end
end

around the curly braces:

code generation only supports cell operations for "varargin" and "varargout"

Another prompt says

Code generation does not support variable "lanes" size growth through indexing

where lanes is mentioned for the second time.

The input Arguments for the function are:

B - Is the output of the bwboundaries Image Processing toolbox function. It is a P-by-1 cell array, where P is the number of objects and holes. Each cell in the cell array contains a Q-by-2 matrix. Each row in the matrix contains the row and column coordinates of a boundary pixel. Q is the number of boundary pixels for the corresponding region.

h - plots the boundaries of the objects with a green outline while being a matrix of size 1 X length(B), holding the values of the boundaries like so like so:

h(K)=plot(boundary(:,2), boundary(:,1), 'g', 'LineWidth', 2);//boundary(:,1) - Y coordinate, boundary(:,2) - X coordinate.

stats - 19x1 struct array acquired using the regionprops function from the Image Processing toolbox with fields: MajorAxisLength and MinorAxisLength (of the object)

I would really appreciate any input you can give in helping me clear this error. Thanks in Advance!

2

There are 2 answers

0
Lokesh A. R. On

Few points about your code generation -

  1. Only a subset of functions in MATLAB and Image Processing Toolbox support code generation - Image Processing Toolbox support for code generation.

  2. Cell arrays do not support code generation yet - Cell array support.

  3. In your code, it seems like your variable is growing i.e. the initial size of the array is not able to support your workflow. You should follow code generation for variable sized inputs.

0
Timothy Sawe On

I had a similar error i.e. code generation does not support variable size growth through indexing. Inside my for loop I had a statement as such which had the same error:

y(i) = k;

I introduced a temporary storage variable u and modified my code to:

u = y;
u(i) = k;
y = u;

I suggest you do the same for your variable lanes.