此示例演示如何通过使用结构,以编程方式配置引用模型的多个实例,以对同一块参数使用不同的值。
当您想要添加、重命名或删除参数时,可以使用结构来减少维护工作。对于结构,模型的数学功能是相同的。
的结构替换参数值ex_model_arg_ref
和ex_model_arg
,请遵循以下步骤。
开放模式ex_model_arg_ref
.该模型代表了一种可重用的算法。
open_system (“ex_model_arg_ref”)
属性中存在的每个参数对象都包含一个字段的结构ex_model_arg_ref
工作区。为每个字段指定一个值。
structForInst1。获得= 3.17;structForInst1。多项式系数= 1.05;
将结构存储在金宝app仿真软件。参数
对象。
structForInst1Param = 金宝appSimulink.Parameter (structForInst1);
复制金宝app仿真软件。参数
对象进ex_model_arg_ref
模型的工作空间。对于本例,命名对象的副本structArg
.
modelWorkspace = get_param (“ex_model_arg_ref”,“ModelWorkspace”);assignin (modelWorkspace“structArg”,复制(structForInst1Param));
配置structArg
作为唯一的模型参数。
set_param (“ex_model_arg_ref”,“ParameterArgumentNames”,“structArg”)
在ex_model_arg_ref
模型,设置获得参数的增益块structArg.gain
并设置分子参数的离散滤波器块structArg.coeff
.
set_param (“ex_model_arg_ref /增益”,“获得”,“structArg.gain”) set_param (“ex_model_arg_ref /离散滤波器”,...“分子”,“structArg.coeff”)
将现有结构复制为structForInst2Param
.
structForInst2Param = (structForInst1Param)复印件;
将这两个结构中的字段值设置为用于在model块中设置模型参数值的相同数字。
structForInst1Param.Value。多项式系数= 0.98;structForInst1Param.Value。获得= 2.98;structForInst2Param.Value。多项式系数= 1.11;structForInst2Param.Value。获得= 3.34;
开放模式ex_model_arg
.此模型表示使用可重用算法的多个实例的系统模型。
open_system (“ex_model_arg”)
对模型实例模型
,设置structArg来structForInst1Param
.对模型实例Model1
,设置structArg来structForInst2Param
.
instSpecParamsStruct = get_param (“ex_model_arg /模式”,“InstanceParameters”);instSpecParamsStruct1 = get_param (“ex_model_arg / Model1”,“InstanceParameters”);instSpecParamsStruct(1)。值=“structForInst1Param”;instSpecParamsStruct1(1)。值=“structForInst2Param”;set_param (“ex_model_arg /模式”,“InstanceParameters”, instSpecParamsStruct);set_param (“ex_model_arg / Model1”,“InstanceParameters”, instSpecParamsStruct1);
你可以用金宝app仿真软件。公共汽车
对象作为结构的数据类型。总线对象确保特定于实例的结构的特征(例如字段的名称和顺序)与模型工作空间中的结构的特征相匹配。
要将结构的数据类型设置为总线对象,请遵循以下步骤。
使用金宝appSimulink.Bus.createObject
函数创建总线对象。对象中元素的层次结构与结构字段的层次结构相匹配。对象的默认名称是slBus1
.
金宝appSimulink.Bus.createObject (structForInst1Param.Value);
通过复制总线对象来重命名它。
myParamStructType = (slBus1)复印件;
使用总线对象设置基本工作区中参数对象的数据类型。
structForInst1Param。数据类型=“巴士:myParamStructType”;structForInst2Param。数据类型=“巴士:myParamStructType”;
为structArg
对象,设置数据类型
来总线:myParamStructType
.
temp = getVariable (modelWorkspace,“structArg”);temp =(临时)复印件;temp.DataType =“巴士:myParamStructType”;assignin (modelWorkspace“structArg”,复制(临时));close_system (“ex_model_arg_ref”, 0) close_system (“ex_model_arg”, 0)