Search code examples
matlabmexmatlab-coder

Defining Variable-size input data for Matlab Coder


I'm trying to generate a mex code file from the .m file using Matlab coder. The code for which is

function [result,x]=tesrank(A,x)

result = [];
n = x;
for col= 1:n
   result = [result, sum(A==col, 2)];
end

For fixed size, I'm able to get it using

codegen tesrank -args {zeros(2,3), zeros(1)}
% Here size(A)=2x3 and size(x)=1x1

How do I do it without limiting the size of A and x?


Solution

  • You do not have to limit the size of the array A.

    Check this example (using Matlab 2014a):

    codegen('funcAccumarray1D_max.m', ...
        '-report', ...
        '-args', {coder.typeof(double(0), [Inf 1]), ...
                  coder.typeof(double(0), [Inf 1])}, ...
        '-o', 'funcAccumarray1D_max')
    

    for this function:

    function [ outs ] = funcAccumarray1D_max(subs, vals, sz) 
    %FUNCACCUMARRAY1D_MAX Construct an array by accumulation using 'max'
    %#codegen
    outs = NaN(sz, 1, 'like', vals);
    for ix=1:size(subs,1)
      sub = subs(ix);
      outs(sub,1) = max(outs(sub,1), vals(ix,1));
    end
    end