【问题标题】:MATLAB Emebedded C function issueMATLAB 嵌入式 C 函数问题
【发布时间】: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 会关闭。文件如下所示: 另外我检查了以下可能的原因:

  1. 已验证 c 中的函数在这种情况下是正确的类型 int
  2. 在 c 代码中使用指针而不是变量。
  3. 在 c 函数中包含 return。
  4. 初始化函数输出的参考值,即 y=int32(15);
  5. 在 c 代码中包含所有标头,即
  6. 链接中的示例完美运行,我缺少什么?

    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


【解决方案1】:

实现这一点的最佳方法是对文件名和函数名非常小心(这就是问题所在,简单、基本但需要时间来发现)。看看我执行的最后一个测试,它工作得很好:

文件是:

//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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-25
    • 2014-02-10
    • 1970-01-01
    • 2014-03-06
    • 1970-01-01
    相关资源
    最近更新 更多