主要内容

通过结构参数通过引用或值在生成的代码

这个例子展示了如何控制结构生成的入口点函数的参数是否通过引用或值。

按引用传递使用一个指针来访问结构参数。如果函数写入输入结构的一个元素,它覆盖输入值。通过值传递复制输入或输出结构的论点。减少内存使用和执行时间,使用以引用的方式传递。

如果一个结构参数是一个输入和输出,生成的入口点函数传递参数的参考。通过结构参数通过引用生成的墨西哥人功能。墨西哥人函数输出,你不能指定你想通过结构参数的值。

指定通过引用或使用MATLAB®编码器的应用价值

打开生成对话框,在生成代码页面,点击生成箭头。

设置构建类型下列之一:

  • 源代码

  • 静态库

  • 动态库

  • 可执行的

点击更多的设置

所有设置选项卡,设置通过结构,参照入口点函数选项:

  • 是的,通过引用传递(默认)

  • 不,按值传递

指定通过引用或值使用命令行接口

静态库的代码创建一个配置对象,动态库,或一个可执行程序。例如,创建一个静态库代码配置对象。

cfg = coder.config(“自由”);

设置PassStructByReference属性:

  • 真正的,通过引用传递(默认)

  • 按值传递

例如:

cfg。PassStructByReference = true;

通过输入结构参数通过引用

编写MATLAB函数my_struct_in一个输入结构参数。

类型my_struct_in.m
函数y = my_struct_in (s) % # codegen y =顺丰速递;

定义一个结构变量mystruct在MATLAB®工作区。

struct mystruct = (“f”1:4);

创建一个代码生成C静态库的配置对象。

cfg = coder.config (“自由”);

指定您想要通过结构参数通过引用。

cfg。PassStructByReference = true;

生成的代码。指定输入参数变量的类型mystruct

codegen配置cfgarg游戏{mystruct}my_struct_in
代码生成成功。

视图生成的C代码。

类型codegen / lib / my_struct_in / my_struct_in.c
/ * *文件:my_struct_in。c * * MATLAB编码器版本:5.6 * c / c++源代码生成:03 - mar - 2023 05:04:36 * / / *包含文件* / # Include " my_struct_in。h my_struct_in_types # include。h”* / / * * / *函数定义参数:const struct0_T * *双y[4] *返回类型:void * /空白my_struct_in (const struct0_T *年代,双y [4]) {y [0] = s - > f [0];y [1] = s - > f [1];y [2] = s - > f [2];y [3] = s - > f [3];}/ * *文件my_struct_in拖车。c * * (EOF) * /

生成的函数签名my_struct_in

空白my_struct_in (const struct0_T *年代,双y [4])

my_struct_in通过输入结构年代通过引用传递。

通过输入结构参数值

指定您想要通过结构参数的值。

cfg。PassStructByReference = false;

生成的代码。指定输入参数变量的类型mystruct

codegen配置cfgarg游戏{mystruct}my_struct_in
代码生成成功。

视图生成的C代码。

类型codegen / lib / my_struct_in / my_struct_in.c
/ * *文件:my_struct_in。c * * MATLAB编码器版本:5.6 * c / c++源代码生成:03 - mar - 2023 05:04:40 * / / *包含文件* / # Include " my_struct_in。h my_struct_in_types # include。h”* / / * * / *函数定义参数:const struct0_T s *双y[4] *返回类型:void * /空白my_struct_in (const struct0_T年代,双y [4]) {y[0] =顺丰速递[0];y[1] =顺丰速递[1];y[2] =顺丰速递[2];y[3] =顺丰速递[3];}/ * *文件my_struct_in拖车。c * * (EOF) * /

生成的函数签名my_struct_in

空白my_struct_in (const struct0_T年代,双y [4]

my_struct_in通过输入结构年代的价值。

以引用的方式传递输出结构参数

编写MATLAB函数my_struct_out一个输出结构参数。

类型my_struct_out.m
函数s = my_struct_out % # codegen s (x)。f = x;

定义一个变量一个在MATLAB®工作区。

一个= 1:4;

创建一个代码生成C静态库的配置对象。

cfg = coder.config (“自由”);

指定您想要通过结构参数通过引用。

cfg。PassStructByReference = true;

生成的代码。指定输入参数变量的类型一个

codegen配置cfgarg游戏{一}my_struct_out
代码生成成功。

视图生成的C代码。

类型codegen / lib / my_struct_out / my_struct_out.c
/ * *文件:my_struct_out。c * * MATLAB编码器版本:5.6 * c / c++源代码生成:03 - mar - 2023 05:04:45 * / / *包含文件* / # Include " my_struct_out。h my_struct_out_types # include。h”* / / * * / *函数定义参数:const双x [4] * struct0_T * *返回类型:void * /空白my_struct_out (const双x [4], struct0_T * s) {s - > f [0] = x [0];s - > f [1] = x [1];s - > f [2] = x [2];s - > f [3] = x [3];}/ * *文件my_struct_out拖车。c * * (EOF) * /

生成的函数签名my_struct_out

空白my_struct_out (const双x [4], struct0_T *年代)

my_struct_out通过输出结构年代通过引用传递。

通过输出结构参数值

指定您想要通过结构参数的值。

cfg。PassStructByReference = false;

生成的代码。指定输入参数变量的类型一个

codegen配置cfgarg游戏{一}my_struct_out
代码生成成功。

视图生成的C代码。

类型codegen / lib / my_struct_out / my_struct_out.c
/ * *文件:my_struct_out。c * * MATLAB编码器版本:5.6 * c / c++源代码生成:03 - mar - 2023 05:04:49 * / / *包含文件* / # Include " my_struct_out。h my_struct_out_types # include。h”* / / * * / *函数定义参数:const双x[4] *返回类型:struct0_T * / struct0_T my_struct_out (const双x [4]) {struct0_T年代;年代。f [0] = x [0];年代。f [1] = x [1];年代。f [2] = x [2];年代。f[3] = x[3]; return s; } /* * File trailer for my_struct_out.c * * [EOF] */

生成的函数签名my_struct_out

struct0_T my_struct_out (const双x [4])

my_struct_out返回一个输出结构。

通过输入和输出结构参数通过引用

当一个参数是一个输入和一个输出,生成的C函数传递参数通过引用即使PassStructByReference是假的。

编写MATLAB函数my_struct_inout有一个结构参数,既是一个输入参数和一个输出参数。

类型my_struct_inout.m
函数[y s] = my_struct_inout (x,年代)% # codegen y = x +总和(顺丰速递);

定义的变量一个和结构变量mystruct在MATLAB®工作区。

一个= 1:4;struct mystruct = (“f”,);

创建一个代码生成C静态库的配置对象。

cfg = coder.config (“自由”);

指定您想要通过结构参数的值。

cfg。PassStructByReference = false;

生成的代码。指定第一个输入的类型一个第二个输入的类型mystruct

codegen配置cfgarg游戏{,mystruct}my_struct_inout
代码生成成功。

视图生成的C代码。

类型codegen / lib / my_struct_inout / my_struct_inout.c
/ * *文件:my_struct_inout。c * * MATLAB编码器版本:5.6 * c / c++源代码生成:03 - mar - 2023 05:04:53 * / / *包含文件* / # Include " my_struct_inout。h my_struct_inout_types # include。h”* / / * * / *函数定义参数:const双x [4] * const struct0_T * *双y[4] *返回类型:void * /空白my_struct_inout (const双x [4], const struct0_T * s,双y[4]){双的话;的话= ((s - > [0] + s - > f [1]) + s - > f [2]) + s - > f [3];y [0] = x[0] +的话;y [1] = x[1] +的话;y [2] = x[2] +的话;y [3] = x[3] +的话;}/ * *文件my_struct_inout拖车。c * * (EOF) * /

生成的函数签名my_struct_inout

空白my_struct_inout (const双x [4], const struct0_T *年代,双y [4])

my_struct_inout通过结构年代通过引用即使PassStructByReference

相关的话题