Matlab环境与Simulink环境下外部函数代码生成问题

首先!matlab外部函数 help文档:https://ww2.mathworks.cn/help/simulink/ug/calling-matlab-functions.html

其次,help文档部分内容不够详细,这里做一些测试。

1、Matlab环境中调试m文件

调用m文件需对应文件名和参数个数。 m文件中第一个函数(主函数)被调用,函数名不需与文件名相同。m文件中定义的其他函数(子函数)可以在第一函数中被调用,不可在文件外被调用。例:xiebian.m

function c = pythagoras(a,b,color) %#codegen
% Calculates the hypotenuse of a right triangle
%  and displays the triangle. 

c = sqrt(a^2 + b^2);
create_plot(a, b, color);

function create_plot(a, b, color)
%Declare patch as extrinsic

coder.extrinsic('patch'); 

x = [0;a;a];
y = [0;0;b];
patch(x, y, color);
axis('equal');

测试: 

>> xiebian(1, 1, [.3 .3 .3])

ans =

    1.4142

这里(matlab环境下)注释掉patch外部函数声明,没有影响。

2、simulink环境调试matlab function

                                                               

Matlab function 中注释掉patch外部函数引用,运行程序,报错如下:

=== Diagnostics (null) ===
    Error: Function 'patch' is not supported for code generation. Consider adding coder.extrinsic('patch') at the top of the function to bypass code generation.
    
    Function 'pythagoras' (#58.291.309), line 15, column 1:
    "patch(x, y, color)"
    Launch diagnostic report.
    Error: Function call failed.
    
    Function 'pythagoras' (#58.146.170), line 6, column 1:
    "create_plot(a, b, color)"
    Launch diagnostic report.
    Error: Errors occurred during parsing of MATLAB function 'pythagoras'

去掉patch外部函数的注释,运行程序:(绿线是matlab function输出结果)

                                                       

查看报错之前的报错信息,patch在非matlab环境中直接调用,不支持代码生成,需声明外部函数。在子函数和主函数报两次错。

额外的测试,外部函数声明范围,将patch声明在主函数将产生同样报错。将子函数并入主函数,并声明则不会报错,程序如下:

function c = pythagoras(a,b,color) %#codegen
% Calculates the hypotenuse of a right triangle
%  and displays the triangle. 
coder.extrinsic('patch'); 
c = sqrt(a^2 + b^2);
patch([0;a;a],[0;0;b],color);
axis('equal');

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值