Borrar filtros
Borrar filtros

Ho to use a dynamic array in a MEX file function

71 visualizaciones (últimos 30 días)
antlhem
antlhem el 29 de Jun. de 2018
Comentada: Ryan Livingston el 17 de Jul. de 2018
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 comentario
Jan
Jan el 12 de Jul. de 2018
It matters if you create a C-Mex file by your own or use codegen for an automatic generation.

Iniciar sesión para comentar.

Respuesta aceptada

Ryan Livingston
Ryan Livingston el 16 de Jul. de 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 comentarios
antlhem
antlhem el 17 de Jul. de 2018
Editada: antlhem el 17 de Jul. de 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!
Ryan Livingston
Ryan Livingston el 17 de Jul. de 2018
You're welcome!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Generating Code en Help Center y File Exchange.

Productos


Versión

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by