MATLAB-用Excel管理Simulink的数据字典-标定量&观测量

前言:

本文旨在帮助大家上手excel管理simulink的数据字典,步骤和代码均是本人掌握之后总结的,代码亲手跑过没有问题,望各位看文章之前先一键三连,多谢。

目的:

得到一个Model_Par_Sig.m的文件,当运行该文件的时候就会将对应的excel表格的signal和Parameter加载至matlab的工作区间,如图1所示:

图1

实现方法:

步骤一:创建excel表格

创建并命名为vtm_var的excel表并按照如下格式编辑表格内容:

图2

图3

图4

这里列举了7个观测量(MEASURE)和3个标定量(CALIBRATION),其中标定量分为数值,一维数组以及二维数组,‘REF’是标定量AVTM_VehSpdFiltCoeff_X_kph和AVTM_MotorSpdFiltCoeff_X_rpm的值的MapData链接,对应的数组值在图3的MapData中,下面讲解如何创建链接:
1)右键MapData中的标定量名,选择‘定义名称’,然后点击‘确定’;

2)切换到CALIBRATION,右键红色标注的地方,选择‘链接’,接着在已定义的名称中选中对应的标定量名称即可建立CALIBRATION和MapData的链接

图5

图6

步骤二:写一个Var.m的脚本

Var.m脚本的主要目的是为了生成Model_Par_Sig.m的脚本文件,并且Model_Par_Sig.m脚本文件的格式如图7,包含Simulink.Signal和Simulink.Parameter的属性,后面的代码段详细备注了Var.m的编码细节。

图7

Var.m的代码如下所示,总共分为两部分,第一部分是导入标定量(CALIBRATION)的数据,第二部分是导入观测量(MEASURE)的数据:

clear;
%%
%创建Model_Par_Sig.m文件并允许写入数据
output_m_file = 'Model_Par_Sig.m';
    fid = fopen(output_m_file,'wt');
%在Model_Par_Sig.m的第一行写入clear命令,用于后面运行Model_Par_Sig.m之前首先清空工作区
fprintf(fid,'clear;\n');
%%%%%%%%%%%%%%%%%%%   导入标定量   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fprintf(fid,'%%导入标定量:\n');%在Model_Par_Sig.m文件中打印‘%导入标定量:’
[~,~,CALIBRATION_Cell] = xlsread('vtm_var','CALIBRATION');%导入CALIBRATION工作表为单元数组


for row=2:size(CALIBRATION_Cell,1)%从CALIBRATION的第2行开始从上之下浏览内容
    ParameterName = CALIBRATION_Cell{row,1};%获取标定量的名称
    
    if ~ischar(ParameterName)%判断名称是不是字符串,是的话继续循环,否则结束
        break;
    end
    
    %在Model_Par_Sig.m打印标定量的参数属性:
    outputParameter_str = strcat(ParameterName,' = ',' Simulink.Parameter',';\n');
    fprintf(fid,outputParameter_str);
    
    %获取标定量的数据类型并在Model_Par_Sig.m文件中按照标准格式打印
    DataType = CALIBRATION_Cell{row,3}; 
    outputParameter_DataType = strcat(ParameterName,'.DataType ',' = ','''',DataType,'''',';\n');
    fprintf(fid,outputParameter_DataType);
    
    %获取标定量Min和Max并在Model_Par_Sig.m文件中按照标准格式打印,注意打印之前首先需要先将数字类型转为字符串
    if isnumeric(CALIBRATION_Cell{row,4})
        Min = num2str(CALIBRATION_Cell{row,4});%将数字类型转为字符串
        outputParameter_Min = strcat(ParameterName,'.Min ',' = ',Min,';\n');
        fprintf(fid,outputParameter_Min);
    end
    
    if isnumeric(CALIBRATION_Cell{row,5})
        Max = num2str(CALIBRATION_Cell{row,5});%将数字类型转为字符串
        outputParameter_Max = strcat(ParameterName,'.Max ',' = ',Max,';\n');
        fprintf(fid,outputParameter_Max);
    end
    
    %获取标定量的单位并在Model_Par_Sig.m文件中按照标准格式打印
    Unit = CALIBRATION_Cell{row,6}; 
    outputParameter_Unit = strcat(ParameterName,'.Unit ',' = ','''',Unit,'''',';\n');
    fprintf(fid,outputParameter_Unit);
    
    %获取标定量的存储类型并在Model_Par_Sig.m文件中按照标准格式打印
    StorageClass = CALIBRATION_Cell{row,8}; 
    outputParameter_StorageClass = strcat(ParameterName,'.CoderInfo.StorageClass ',' = ','''',StorageClass,'''',';\n');
    fprintf(fid,outputParameter_StorageClass);
    
    %获取标定量的备注并在Model_Par_Sig.m文件中按照标准格式打印
    Description = CALIBRATION_Cell{row,9}; 
    outputParameter_Description = strcat(ParameterName,'.Description ',' = ','''',Description,'''',';\n');
    fprintf(fid,outputParameter_Description);

    %获取标定量的值
    Val = CALIBRATION_Cell{row,2};
    
     %标定量的数字是一个非数组的数据,则在Model_Par_Sig.m文件中按照标准格式打印
     if isnumeric(Val)
        VALUE = num2str(Val);
        outputParameter_VALUE = strcat(ParameterName,'.Value ',' = ',VALUE,';\n');
        fprintf(fid,outputParameter_VALUE);
     else
         %标定量的值是数组,则在MapData中搜寻与CALIBRATION相同的名字并获取数组的数据
         if strcmp('REF',string(Val))
                [~,~,Mapdata_Cell] = xlsread('vtm_var','MapData');
                MapData_row = size(Mapdata_Cell,1);%获取MapData行数
                
                %对表格的值进行循环赋值,分别对一维表格和二维表格进行处理
                for row=1:MapData_row
                    MapDataName = Mapdata_Cell{row,1};
                    
                    if isnan(MapDataName)
                        continue;                     
                    else
                        if ischar(MapDataName) && ~isnumeric(MapDataName)
                            if strcmp(ParameterName, MapDataName)

                                %numRow用来计算数组的行数,numRow==1表示一维数组,大于1则为多维数组 
                                numRow = 0;                                                       
                                for W_row=row+1:MapData_row
                                    if isnumeric(Mapdata_Cell{W_row,1}) && ~isnan(Mapdata_Cell{W_row,1})
                                       numRow = numRow+1;
                                       continue;
                                    else
                                        break;
                                    end
                                end
                                 
                                %Model_Par_Sig.m文件中按照标准格式打印一维数组的值
                                if numRow == 1
                                    outputParameter_VALUE = strcat(ParameterName,'.Value ',' = ','[');
                                    fprintf(fid,outputParameter_VALUE);
                                    
                                    for array_colume=1:size(Mapdata_Cell,2)
                                        if ~isnan(Mapdata_Cell{row+1,array_colume})
                                            VALUE = num2str(Mapdata_Cell{row+1,array_colume});
                                            outputParameter_VALUE = strcat(VALUE,'\40');
                                            fprintf(fid,outputParameter_VALUE);
                                        else
                                            continue;
                                        end
                                    end
                                    fprintf(fid,'];\n');

                                %Model_Par_Sig.m文件中按照标准格式打印二维数组的值
                                else
                                    outputParameter_VALUE = strcat(ParameterName,'.Value ',' = ','[');
                                    fprintf(fid,outputParameter_VALUE);
                                    for array_row=1:numRow
                                        for array_colume=1:size(Mapdata_Cell,2)                    
                                            if ~isnan(Mapdata_Cell{row+array_row,array_colume})
                                                VALUE = num2str(Mapdata_Cell{row+array_row,array_colume});
                                                outputParameter_VALUE = strcat(VALUE,'\40');
                                                fprintf(fid,outputParameter_VALUE);
                                            else
                                                break;
                                            end
                                        end
                                        
                                        if array_row ~= numRow
                                            fprintf(fid,'; ');
                                        else
                                            fprintf(fid,'];');
                                        end
                                    end
                                end
                            end
                        else                           
                        end
                    end
                end       
          end
    end 
    fprintf(fid,'\n');
end
%%
%%%%%%%%%%%%%%%%%%%   导入观测量   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fprintf(fid,'%%导入观测量:\n');
[~,~,MEASURE_Cell] = xlsread('vtm_var','MEASURE');   %导入MEASURE工作表为单元数组
for row = 2:size(MEASURE_Cell,1) %从第2行开始循环处理每一行内容    
    MEASURE_Name = MEASURE_Cell{row,2}; % 提取信号名
    
    %在Model_Par_Sig.m打印观测量的信号属性:
    if ischar(MEASURE_Name)
        outputParameter_str = strcat(MEASURE_Name,' = ',' Simulink.Signal',';\n');
        fprintf(fid,outputParameter_str);
        
        %获取观测量的初始值并在Model_Par_Sig.m文件中按照标准格式打印
        if isnumeric(MEASURE_Cell{row,3})
            InitialValue = num2str(MEASURE_Cell{row,3});
            outputParameter_InitialValue = strcat(MEASURE_Name,'.InitialValue ',' = ','''',InitialValue,'''',';\n');
            fprintf(fid,outputParameter_InitialValue);
        end
        
        %获取观测量Min和Max并在Model_Par_Sig.m文件中按照标准格式打印,注意打印之前首先需要先将数字类型转为字符串
        if isnumeric(MEASURE_Cell{row,4})
            Min = num2str(MEASURE_Cell{row,4});
            outputParameter_Min = strcat(MEASURE_Name,'.Min ',' = ',Min,';\n');
            fprintf(fid,outputParameter_Min);
        end
        
        if isnumeric(MEASURE_Cell{row,5})
            Max = num2str(MEASURE_Cell{row,5});
            outputParameter_Max = strcat(MEASURE_Name,'.Max ',' = ',Max,';\n');
            fprintf(fid,outputParameter_Max);
        end
        
        %获取观测量的数据类型并在Model_Par_Sig.m文件中按照标准格式打印
        DataType = MEASURE_Cell{row,6}; 
        outputParameter_DataType = strcat(MEASURE_Name,'.DataType ',' = ','''',DataType,'''',';\n');
        fprintf(fid,outputParameter_DataType);   

        %获取观测量的单位并在Model_Par_Sig.m文件中按照标准格式打印
        Unit = MEASURE_Cell{row,7}; 
        outputParameter_Unit = strcat(MEASURE_Name,'.Unit ',' = ','''',Unit,'''',';\n');
        fprintf(fid,outputParameter_Unit);

        %获取观测量的存储类型并在Model_Par_Sig.m文件中按照标准格式打印
        StorageClass = MEASURE_Cell{row,8}; 
        outputParameter_StorageClass = strcat(MEASURE_Name,'.CoderInfo.StorageClass ',' = ','''',StorageClass,'''',';\n');
        fprintf(fid,outputParameter_StorageClass);

        %获取观测量的备注并在Model_Par_Sig.m文件中按照标准格式打印
        Description = MEASURE_Cell{row,10}; 
        outputParameter_Description = strcat(MEASURE_Name,'.Description ',' = ','''',Description,'''',';\n');
        fprintf(fid,outputParameter_Description);
        fprintf(fid,'\n');
    end
end

run('Model_Par_Sig.m');%运行生成的Model_Par_Sig.m脚本文件
clc;%清除命令行窗口

步骤三:运行Var.m

运行Var.m即可生成Model_Par_Sig.m,并成功在工作区创建excel对应的标定量和观测量的属性,其中生成的Model_Par_Sig.m文件代码如下:

clear;
%导入标定量:
KVTM_BrkPedReld_pct = Simulink.Parameter;
KVTM_BrkPedReld_pct.DataType ='single';
KVTM_BrkPedReld_pct.Min =-100;
KVTM_BrkPedReld_pct.Max =200;
KVTM_BrkPedReld_pct.Unit ='pct';
KVTM_BrkPedReld_pct.CoderInfo.StorageClass ='ExportedGlobal';
KVTM_BrkPedReld_pct.Description ='Thir';
KVTM_BrkPedReld_pct.Value =2;

AVTM_VehSpdFiltCoeff_X_kph = Simulink.Parameter;
AVTM_VehSpdFiltCoeff_X_kph.DataType ='single';
AVTM_VehSpdFiltCoeff_X_kph.Min =-300;
AVTM_VehSpdFiltCoeff_X_kph.Max =300;
AVTM_VehSpdFiltCoeff_X_kph.Unit ='kph';
AVTM_VehSpdFiltCoeff_X_kph.CoderInfo.StorageClass ='ExportedGlobal';
AVTM_VehSpdFiltCoeff_X_kph.Description ='fir';
AVTM_VehSpdFiltCoeff_X_kph.Value =[5 0.5 1 2 3 4 ];

AVTM_MotorSpdFiltCoeff_X_rpm = Simulink.Parameter;
AVTM_MotorSpdFiltCoeff_X_rpm.DataType ='single';
AVTM_MotorSpdFiltCoeff_X_rpm.Min =-10000;
AVTM_MotorSpdFiltCoeff_X_rpm.Max =10000;
AVTM_MotorSpdFiltCoeff_X_rpm.Unit ='rpm';
AVTM_MotorSpdFiltCoeff_X_rpm.CoderInfo.StorageClass ='ExportedGlobal';
AVTM_MotorSpdFiltCoeff_X_rpm.Description ='sec';
AVTM_MotorSpdFiltCoeff_X_rpm.Value =[0 10 20 30 50 100 200 300 ; 0 20 20 20 20 20 20 20 ];
%导入观测量:
VVTM_VehSpd_kph = Simulink.Signal;
VVTM_VehSpd_kph.InitialValue ='0';
VVTM_VehSpd_kph.Min =-10000;
VVTM_VehSpd_kph.DataType ='single';
VVTM_VehSpd_kph.Unit ='kph';
VVTM_VehSpd_kph.CoderInfo.StorageClass ='ExportedGlobal';
VVTM_VehSpd_kph.Description ='ret';

VVTM_MotorSpd_rpm = Simulink.Signal;
VVTM_MotorSpd_rpm.InitialValue ='0';
VVTM_MotorSpd_rpm.Min =-100000;
VVTM_MotorSpd_rpm.Max =100000;
VVTM_MotorSpd_rpm.DataType ='single';
VVTM_MotorSpd_rpm.Unit ='rpm';
VVTM_MotorSpd_rpm.CoderInfo.StorageClass ='ExportedGlobal';
VVTM_MotorSpd_rpm.Description ='frg';

VVTM_VehActGrPstn_enum = Simulink.Signal;
VVTM_VehActGrPstn_enum.InitialValue ='0';
VVTM_VehActGrPstn_enum.Min =0;
VVTM_VehActGrPstn_enum.Max =255;
VVTM_VehActGrPstn_enum.DataType ='uint8';
VVTM_VehActGrPstn_enum.Unit ='enum';
VVTM_VehActGrPstn_enum.CoderInfo.StorageClass ='ExportedGlobal';
VVTM_VehActGrPstn_enum.Description ='';
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值