【发布时间】:2018-12-05 14:35:09
【问题描述】:
您好,我正在尝试通过 MEX 文件在 MATLAB 中使用 C 文件,但到目前为止还没有成功。我按照此链接中显示的教程进行操作:https://uk.mathworks.com/videos/integrating-matlab-into-your-cc-product-development-workflow-106742.html 我下载了文件并且工作得很好。现在我正在尝试编写自己的函数,它会正确生成 MEX 文件,但是当我尝试测试函数时,MATLAB 会关闭。文件如下所示: 另外我检查了以下可能的原因:
- 已验证 c 中的函数在这种情况下是正确的类型 int。
- 在 c 代码中使用指针而不是变量。
- 在 c 函数中包含 return。
- 初始化函数输出的参考值,即 y=int32(15);
- 在 c 代码中包含所有标头,即
-
链接中的示例完美运行,我缺少什么?
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();
【问题讨论】:
-
尝试在您的 MATLAB 代码中添加
coder.cinclude调用,以包含声明bimi的标头。您应该看到关于没有声明的编译警告。此外,您的bimi返回一个指针而不是指向的值。它应该返回*output吗?
标签: c matlab matlab-coder