Ho to use a dynamic array in a MEX file function

14 visualizzazioni (ultimi 30 giorni)
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 Commento
Jan
Jan il 12 Lug 2018
It matters if you create a C-Mex file by your own or use codegen for an automatic generation.

Accedi per commentare.

Risposta accettata

Ryan Livingston
Ryan Livingston il 16 Lug 2018
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 Commenti
antlhem
antlhem il 17 Lug 2018
Modificato: antlhem il 17 Lug 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!

Accedi per commentare.

Più risposte (0)

Prodotti


Release

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by