switch

您所在的位置:网站首页 怀念爱人逝世的唯美句子图片 switch

switch

#switch | 来源: 网络整理| 查看: 265

C 构造 switch (u1) { case 2: y1 = u2; break; case 3: u3; break; default: y1 = u4; break; } switch 的建模模式:Switch Case 模块

创建 switch 语句的一种方法是使用 Simulink > 信号路由库中的 Switch Case 模块。

1.打开示例模型 ex_switch_SL。

该模型包含一个 Switch Case Action Subsystem。Switch Case 模块接受整数输入,因此,输入信号 u1 的类型会转换为 int32。

2.要编译模型并生成代码,请按 Ctrl+B。

实现 switch 构造的代码在 ex_switch_SL.c 中的 ex_switch_SL_step 函数中:

/* Exported block signals */ int32_T u1; /* '/u1' */ /* External inputs (root inport signals with default storage) */ ExternalInputs rtU; /* External outputs (root outports fed by signals with default storage) */ ExternalOutputs rtY; /* Model step function */ void ex_switch_SL_step(void) { /* SwitchCase: '/Switch Case' incorporates: * Inport: '/u1' */ switch (u1) { case 2: /* Outputs for IfAction SubSystem: '/Switch Case Action Subsystem' incorporates: * ActionPort: '/Action Port' */ /* Outport: '/y1' incorporates: * Inport: '/u2' * SignalConversion generated from: '/u2' */ rtY.y1 = rtU.u2; /* End of Outputs for SubSystem: '/Switch Case Action Subsystem' */ break; case 3: /* Outputs for IfAction SubSystem: '/Switch Case Action Subsystem1' incorporates: * ActionPort: '/Action Port' */ /* Outport: '/y1' incorporates: * Inport: '/u3' * SignalConversion generated from: '/u3' */ rtY.y1 = rtU.u3; /* End of Outputs for SubSystem: '/Switch Case Action Subsystem1' */ break; default: /* Outputs for IfAction SubSystem: '/Switch Case Action Subsystem2' incorporates: * ActionPort: '/Action Port' */ /* Outport: '/y1' incorporates: * Inport: '/u4' * SignalConversion generated from: '/u4' */ rtY.y1 = rtU.u4; /* End of Outputs for SubSystem: '/Switch Case Action Subsystem2' */ break; } /* End of SwitchCase: '/Switch Case' */ } switch 的建模模式:MATLAB Function 模块

1.打开示例模型 ex_switch_ML。

MATLAB Function 模块包含以下函数:

function y1 = fcn(u1, u2, u3, u4) switch u1 case 2 y1 = u2; case 3 y1 = u3; otherwise y1 = u4; end

2.要编译模型并生成代码,请按 Ctrl+B。

实现 switch 构造的代码在 ex_switch_ML.c 中的 ex_switch_ML_step 函数中:

/* External inputs (root inport signals with default storage) */ ExternalInputs rtU; /* External outputs (root outports fed by signals with default storage) */ ExternalOutputs rtY; /* Model step function */ void ex_switch_ML_step(void) { /* MATLAB Function: '/MATLAB Function' incorporates: * Inport: '/u1' */ switch (rtU.u1) { case 2: /* Outport: '/y1' incorporates: * Inport: '/u2' */ rtY.y1 = rtU.u2; break; case 3: /* Outport: '/y1' incorporates: * Inport: '/u3' */ rtY.y1 = rtU.u3; break; default: /* Outport: '/y1' incorporates: * Inport: '/u4' */ rtY.y1 = rtU.u4; break; } /* End of MATLAB Function: '/MATLAB Function' */ } 将 If-Elseif-Else 转换为 Switch 语句

如果一个 MATLAB Function 模块或 Stateflow 图使用 if-elseif-else 决策逻辑,您可以通过使用配置参数将该模块或图转换为 switch 语句。选择配置参数 > 代码生成 > 代码样式 > Convert if-elseif-else patterns to switch-case statements 参数。有关详细信息,请参阅Converting If-Elseif-Else Code to Switch-Case Statements。有关使用 Stateflow 图进行此转换的详细信息,请参阅Enhance Readability of Code for Flow Charts。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3