前言
这个是通过表格数据生成模型接口的函数,缩减了在平常建模时繁琐的复制,可以参考使用,
首先:
在第一章提到的DBC–EXCEL中生成的最后作品“
接下来的函数就是通过上图信息将其生成模型
下面看下最终结果
其中包括输入输出的类型。中间模块的参数(系数,偏移量)都已经写入相对应的模块中
函数
说下具体所用到的函数:
uigetfile函数:
函数 | uigetfile(filter,title) |
---|---|
参数 | 文件筐,选择文件 |
示例 | [filetxt,pathtxt] = uigetfile(‘.’,‘Please select an image’); |
xlsread函数
函数 | xlsread() |
---|---|
参数 | xlsread(filename) 读取名为 filename 的电子表格, |
参数 | xlsread(filename,sheet) 读取指定的工作表。 |
参数 | xlsread(filename,xlRange) 从工作簿的第一个工作表的指定范围内读取数据。使用 Excel 范围语法,例如 ‘A1:C3’。 |
参数 | xlsread(filename,sheet,xlRange) 读取指定的工作表和范围。 |
参数 | xlsread(filename,sheet,xlRange,‘basic’) 在 basic 导入模式下读取电子表格中的数据。 |
示例 | [data,txt]=xlsread(filetxt)调取文件filetxt下边的data和txt类型数据 |
cellfun函数
函数 | cellfun(func,C) |
---|---|
参数 | 判断数列A是否为空,返回值 |
示例 | cellfun(@isempty,txt |
strcmp函数
函数 | strcmp(A,B) |
---|---|
参数 | A和B两个文本数值 |
示例 | string(A(i,2));判断两个是是否相等 |
append函数
函数 | str = append(str1,…,strN) |
---|---|
参数 | 文本 |
示例 | append(‘hld’,‘‘,txt{i,15},’’,txt{i,10})合并文本 |
set_param函数
函数 | set_param(Object,ParameterName,Value) |
---|---|
参数 | (模块地址,参数,数值) |
示例 | set_param(zzz,‘factor’,ll)% |
add_line函数
函数 | add_line(sys,out,in) |
---|---|
参数 | (模型名称,信号输出模块,信号接入模块) |
示例 | add_line(‘connect_model’,‘Constant/1’,‘Gain/1’);1代表接口名称 |
strcat函数
函数 | strcat(pathtxt,filetxt); |
---|---|
参数 | (文本,文本) |
注释 | 将文本进行合并 |
示例 | strcat(pathtxt,filetxt); |
代码展示
clear all; close all; clc
[filetxt,pathtxt] = uigetfile('*.*','Please select an image');%文件筐,选择文件
if(filetxt)
filetxt = strcat(pathtxt,filetxt);
filetxt = lower(filetxt);%一致的小写字母形式
else
msgbox('Please select an image');
return; %退出程序
end
new_system('Can_Input') % 创建模型
open_system('Can_Input') % 打开模型
[data,txt]=xlsread(filetxt);%读取文件数据函数
C = readmatrix(filetxt);%读取文件数据函数
A=~cellfun(@isempty,txt);%定位有效数据
inportLength=sum(A(:,10));%共计
for i=2:inportLength %循环语句
str = string(A(i,2)); %查询(i,2)位置数据是不是有效数据
if (strcmp(str,'false')) % 如果无效则执行以下语句
strd = append('hld','_',txt{i,15},'_',txt{i,10});%信号名称,个人要求
add_block('simulink/Commonly Used Blocks/In1',strcat('Can_Input/',strd),'Position',[30 100+i*50 60 115+i*50]);添加到模型位置 add_block(模块,模块命名,创建位置)
ddd=strcat('Can_Input','/',strd);%模块地址
set_param(ddd,'OutDataTypeStr',txt{i,15})%修改模块属性
add_block('platform_lib/communicate_model',strcat('Can_Input/',txt{i,10}),'Position',[150 100+i*50 210 115+i*50])%这个库的路径是作者的,使用人看情况需求而定
ll = string(C(i-1,14));%转文本
hh = string(C(i-1,16));%转文本
zzz=strcat('Can_Input','/',txt{i,10});%模块地址
set_param(zzz,'factor',ll)%修改模块属性
set_param(zzz,'offset',hh)%修改模块属性
strde = append(txt{i,15},'_',txt{i,10});%信号名称,个人要求
add_block('simulink/Commonly Used Blocks/Out1',strcat('Can_Input/',strde),'Position',[290 100+i*50 320 115+i*50]);添加到模型位置 add_block(模块,模块命名,创建位置)
fff=strcat('Can_Input','/',strde);
set_param(fff,'OutDataTypeStr',txt{i,15})
aaa=strcat(append('hld','_',txt{i,15},'_',txt{i,10}),'/','1');%名称合并(1代表信号接口)
bbb=strcat(txt{i,10},'/','1');
ccc=strcat(append(txt{i,15},'_',txt{i,10}),'/','1');
add_line('Can_Input', aaa, bbb)%连线
add_line('Can_Input', bbb, ccc)
else
end
end