嗨,我正在尝试通过MEX文件在MATLAB中使用C文件,但到目前为止还算不上什么。我遵循了此链接中显示的教程:https://uk.mathworks.com/videos/integrating-matlab-into-your-cc-product-development-workflow-106742.html 我下载了文件,并且工作正常。现在,我正在尝试编写自己的函数,并且可以正确生成MEX文件,但是当我尝试测试该函数时,MATLAB将关闭。这些文件如下所示: 另外,我检查了以下可能的原因:
链接中的示例运行正常,我缺少了什么?
C代码
#include <stdio.h>
int bimi(int* output);
int bimi(int* output){
int a = 0;
output=25;
return output;
}
MATLAB中的函数
function y = bimi() %#codegen
%coder.updateBuildInfo('addSourceFiles','bm.c');
y = int32(15);%coder.nullcopy(1);
coder.updateBuildInfo('addSourceFiles','bimi.c');
fprintf('Running Custom C Code...\n\n');
coder.ceval('bimi',coder.ref(y));%this bimi should match with the function name!
end
用于生成MEX文件的脚本
function build(target)
% BUILD is a build function for the Gaussian filter
%
% SYNTAX: build mex
% build lib
%
% Copyright 2014 The MathWorks, Inc.
% Entry point function:
entryPoint = 'bimi';%
% Configuration object:
cfg = coder.config(target);
% Custom source files:
cfg.CustomSource = 'bimi.c';
% Generate and Launch Report:
cfg.GenerateReport = true;
cfg.LaunchReport = false;
% Generate Code:
codegen(entryPoint,'-config', cfg)
end
测试脚本
%testing the c code embeded to matlab
chichi=bimi_mex();
答案 0 :(得分:0)
实现此目标的最佳方法是非常小心文件名和函数名(这是问题,简单,基本,但要花些时间才能发现)。看一下我执行的最后一次测试,结果很好:
文件为:
//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
%coder.updateBuildInfo('addSourceFiles','bm.c');
y = coder.nullcopy(zeros(1,5,'int32'));%coder.nullcopy(1);
coder.updateBuildInfo('addSourceFiles','bimi.c');
fprintf('Running Custom C Code...\n\n');
coder.ceval('bimi',coder.ref(poly2),coder.ref(polysz2), coder.ref(y));%this bimi should match with the function name!
end
。
//build.m
function build(target)
% Entry point function:
entryPoint = 'bimifunc';%
%Example input
poly=zeros(1,5, 'int32');
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
%testing the c code embeded to matlab
poly=ones(1,5, 'int32');
polysz=int32(length(poly));
chichi=bimifunc_mex(poly, polysz);