필터 지우기
필터 지우기

Ho to use a dynamic array in a MEX file function

조회 수: 64 (최근 30일)
antlhem
antlhem 2018년 6월 29일
댓글: Ryan Livingston 2018년 7월 17일
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
Jan
Jan 2018년 7월 12일
It matters if you create a C-Mex file by your own or use codegen for an automatic generation.

댓글을 달려면 로그인하십시오.

채택된 답변

Ryan Livingston
Ryan Livingston 2018년 7월 16일
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
antlhem
antlhem 2018년 7월 17일
편집: antlhem 2018년 7월 17일
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 2018년 7월 17일
You're welcome!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Generating Code에 대해 자세히 알아보기

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by