Ho to use a dynamic array in a MEX file function

Hi I am using a C function in my MATLAB project and one of the inputs is a dynamic array, how can I modify this input since in build.m the inputs are set up and fixed. I want to be able to throw any array size in to it. Following you can find the files I am using:
bimi.c
#include "bimi.h"
extern void bimi(int* poly, int* polysz, int* output){
int i;
int a=*(polysz);
for(i=0;i<a;i++){
output[i]=2*poly[i];
}
}
bimi.h
extern void bimi(int* poly, int* polysz, int* output);
bimifunc.m
function y = bimifunc(poly2, polysz2) %#codegen
y = coder.nullcopy(zeros(1,5,'int32'));
coder.updateBuildInfo('addSourceFiles','bimi.c');
fprintf('Running Custom C Code...\n\n');
coder.ceval('bimi',coder.ref(poly2),coder.ref(polysz2), coder.ref(y));
end
build.m, this where the input size is setup and fixed, how can I make it dynamic?
function build(target)
% Entry point function:
entryPoint = 'bimifunc';%
%Example input
poly=zeros(1,5, 'int32');%THIS WHERE I WANT THE DYNAMIC ARRAY OR IS THERE ANOTHER WAY?
polysz=int32(length(poly));*
% Configuration object:
cfg = coder.config(target);
% Custom source files:
cfg.CustomSource = 'bimi.c';
cfg.CustomSourceCode = [ '#include "bimi.h"' ];
% Generate and Launch Report:
cfg.GenerateReport = true;
cfg.LaunchReport = false;
% Generate Code:
codegen(entryPoint,'-args', {poly, polysz},'-config', cfg)
end
test.m, I want to be able to generate any random size of array and throw it to the function for calculations.
%testing the c code embeded to matlab
poly=ones(1,5, 'int32');
polysz=int32(length(poly));
chichi=bimifunc_mex(poly, polysz);

1 Comment

It matters if you create a C-Mex file by your own or use codegen for an automatic generation.

Sign in to comment.

 Accepted Answer

Update your code:
function y = bimifunc(poly2, polysz2) %#codegen
%coder.updateBuildInfo('addSourceFiles','bm.c');
y = coder.nullcopy(zeros(1,polysz2,'int32'));
and the codegen command to use a variable size input:
codegen(entryPoint,'-args', {coder.typeof(poly,[1,Inf]), polysz},'-config', cfg)
That will make the input, poly a 1-by-:Inf array and allocate the output y based on the size of polysz.
Another suggestion would be rather than taking polysz as an argument to your MATLAB function, should it just be:
function y = bimifunc(poly2) %#codegen
%coder.updateBuildInfo('addSourceFiles','bm.c');
polysz2 = int32(length(polysz2));
y = coder.nullcopy(zeros(1,polysz2,'int32'));
and then:
codegen(entryPoint,'-args', {coder.typeof(poly,[1,Inf])},'-config', cfg)

4 Comments

antlhem
antlhem on 17 Jul 2018
Edited: antlhem on 17 Jul 2018
do you suggest me to join build.m with bimifunc.m? or how do I initialise poly and polysz to be used in the codegen?. Or just leave the lines of example input? So far the ERROR message I am getting is: _ Undefined function or variable 'poly'._ this error is from build.m
build.m and bimifunc.m need to be separate files. I'm just suggesting that you replace the code in bimifunc with the first block I showed and replace your call to codegen in build.m with the second block. Give that a try and see if it works without changing anything else.
antlhem
antlhem on 17 Jul 2018
Edited: antlhem on 17 Jul 2018
OK. I left the example input line and added the codegen that you suggested additionally I updated y, without joining the files. Thank you!

Sign in to comment.

More Answers (0)

Products

Release

R2017a

Community Treasure Hunt

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

Start Hunting!