主要内容

使用C调用块条件执行

这个例子展示了如何使用一个C调用块条件执行。示例模型包含两个C调用程序块的输出输入分支开关部件。每个C调用者调用自定义的C代码块功能sort_ascend ()sort_descend ()。只有一个值的输入分支传递给输出开关部件。一个C函数块相同的设置也可以使用代替C调用代码块调用这些自定义函数。

这个例子说明了以下概念:

  • 调用自定义C代码从一个仿真软件®块在一个条件执行上下文。金宝app

  • 配置确定的函数功能的自定义代码。

检查模型

该模型通过C调用者调用自定义代码块,连接到一个开关的输入分支块。

open_system (“slexCCallerConditional”);%创建一个临时文件夹的建立和检验过程。currentDir = pwd;[~,cgDir] = rtwdemodir ();

生成代码

自定义代码的功能在这个模型没有被确认为确定性函数。当代码生成自模型,C函数执行在模拟和生成的代码,即使开关组需要的输出只有一个函数。

slbuild (bdroot);用= fullfile (cgDir,“slexCCallerConditional_grt_rtw”,“slexCCallerConditional.c”);rtwdemodbtype(用“/ *模型步骤”,“/ * Matfile日志”1 0);
# # #开始构建过程:slexCCallerConditional # # #成功完成构建过程:slexCCallerConditional模型建立目标:总结构建模型重建行动的原因= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = slexCCallerConditional代码生成和编译。代码生成信息文件不存在。1 1模型的建立(0模型已经更新)构建持续时间:0 h 0米34.894秒/ *模型阶跃函数* /空白slexCCallerConditional_step (void) {real_T rtb_CCallerascend [6];real_T rtb_CCallerdescend [6];int32_T我;/ * CCaller:“< Root > / C调用者(提升)包含:*尺寸:“< Root > / In2”* / (i = 0;我< 6;我+ +){rtb_CCallerascend[我]= slexCCallerConditional_U.In2[我];}sort_ascend (&rtb_CCallerascend [0], 6);/ * CCaller结束:“< Root > / C调用者(提升)* / / * CCaller: ' < Root > / C调用者(下)包含:*尺寸:“< Root > / In2”* / (i = 0; i < 6; i++) { rtb_CCallerdescend[i] = slexCCallerConditional_U.In2[i]; } sort_descend(&rtb_CCallerdescend[0], 6); /* End of CCaller: '/C Caller (descend)' */ for (i = 0; i < 6; i++) { /* Switch: '/Switch' incorporates: * Inport: '/In1' */ if (slexCCallerConditional_U.In1 > 0.0) { /* Outport: '/Out3' */ slexCCallerConditional_Y.Out3[i] = rtb_CCallerascend[i]; } else { /* Outport: '/Out3' */ slexCCallerConditional_Y.Out3[i] = rtb_CCallerdescend[i]; } /* End of Switch: '/Switch' */ }

指定自定义代码确定的功能

打开配置参数对话框。

模拟目标窗格中,设置确定的功能通过函数。点击指定的功能和添加功能sort_ascendsort_descend到列表中。这个动作告诉模型指定的自定义代码函数确定的行为,也就是说,同样的输入值的函数总是给相同的输出。如果一个函数将是决定性的,它不需要被称为如果是在输入分支对应于一个错误的开关组的价值。

configset.highlightParameter (bdroot“DefaultCustomCodeDeterministicFunctions”);set_param (bdroot“DefaultCustomCodeDeterministicFunctions”,“ByFunction”);set_param (bdroot“CustomCodeDeterministicFunctions”,“sort_ascend, sort_descend”);

重新生成代码

现在您已经指定了确定性函数,生成的代码效率更高,因为只有C调用者在执行开关组的真正的分支。该效率同样适用于当你在仿真软件模拟模型。金宝app

slbuild (bdroot);用= fullfile (cgDir,“slexCCallerConditional_grt_rtw”,“slexCCallerConditional.c”);rtwdemodbtype(用“/ *模型步骤”,“/ * Matfile日志”1 0);close_system (bdroot 0);
# # #开始构建过程:slexCCallerConditional # # #成功完成构建过程:slexCCallerConditional模型建立目标:总结构建模型重建行动的原因= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = slexCCallerConditional代码生成和编译。生成的代码是过时了。1 1模型的建立(0模型已经更新)构建持续时间:0 h 0米23.207秒/ *模型阶跃函数* /空白slexCCallerConditional_step (void) {real_T rtb_CCallerascend [6];int32_T我;/ *开关:“< Root > /开关”包含:*尺寸:“< Root > / In1”如果(slexCCallerConditional_U * /。In1 > 0.0) {/ * CCaller:“< Root > / C调用者(提升)包含:*尺寸:“< Root > / In2”* / (i = 0;我< 6;我+ +){rtb_CCallerascend[我]= slexCCallerConditional_U.In2[我];}sort_ascend (&rtb_CCallerascend [0], 6);/ * CCaller结束:“< Root > / C调用者(提升)的其他* /}{/ * CCaller: ' < Root > / C调用者(下)包含:*尺寸:“< Root > / In2”* / (i = 0; i < 6; i++) { rtb_CCallerascend[i] = slexCCallerConditional_U.In2[i]; } sort_descend(&rtb_CCallerascend[0], 6); /* End of CCaller: '/C Caller (descend)' */ } /* End of Switch: '/Switch' */ /* Outport: '/Out3' */ for (i = 0; i < 6; i++) { slexCCallerConditional_Y.Out3[i] = rtb_CCallerascend[i]; } /* End of Outport: '/Out3' */

另请参阅

|||