bus在matlab里哪里,Matlab中使用脚本和xml文件自动生成bus模块

这篇博客介绍了如何利用MATLAB函数从XML配置文件读取总线信号定义,自动生成Simulink BusObject和Bus模块库。通过这种方式,可以简化和规范接口设计,方便后期修改。博客提供了具体的MATLAB代码示例,生成的模型符合预期。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

帮一个老师写的小工具

在一个大工程中需要很多bus来组织信号,而为了规范接口,需要定义很多BusObject,用Matlab语言手写这些BusObject比较费工夫

所以用xml配置文件来写,也便于更改总线数据接口,然后使用matlab脚本来生成BusObject和Bus模块库

以下代码运行环境:WIN10+Matlab2015a

下面给出代码Matlab函数的代码:

function xmlbuscreator(xmlfile)

% XMLBUSCREATOR:从xml文件读取数据结构,并生成bus

% xmlbuscreator(xmlfile):从xmlfile文件读取bus定义,并生成总线

%% 读取文件并提取信号

try

xdoc = xmlread(xmlfile);

catch

error(‘读取XML文件:%s失败.‘,xmlfile);

end

BusLibElement = xdoc.getElementsByTagName(‘BusLib‘);

BusLibItem = BusLibElement.item(0);

BusLibName = BusLibItem.getAttribute(‘Name‘);

BusObjList = BusLibItem.getElementsByTagName(‘BusObject‘);

BusObjNum = BusObjList.getLength;

BusInfo = cell(BusObjNum,1);

for i = 1:BusObjNum

BusObj = BusObjList.item(i-1);

BusName = char(BusObj.getAttribute(‘Name‘));

BusElementsList = BusObj.getElementsByTagName(‘BusElement‘);

ElementNum = BusElementsList.getLength;

% 如何分配一个结构体数组,BusElements = struct();

for j = 1:ElementNum

ele = BusElementsList.item(j-1);

BusElements(j) = Simulink.BusElement;

try

BusElements(j).Name = char(ele.getAttribute(‘Name‘));

BusElements(j).DataType = char(ele.getAttribute(‘DataType‘));

BusElements(j).Dimensions = str2double(ele.getAttribute(‘Dimensions‘));

BusElements(j).SampleTime = -1;

BusElements(j).Complexity = ‘real‘;

BusElements(j).SamplingMode = ‘Sample based‘;

catch me

error(me.message);

end

end

BusObject = Simulink.Bus;

BusObject.HeaderFile = ‘‘;%默认无头文件

BusObject.Description = ‘‘;

BusObject.DataScope = ‘Auto‘;

BusObject.Alignment = -1;

BusObject.Elements = BusElements;

assignin(‘base‘,BusName,BusObject);

BusInfo{i}.BusName = BusName;

BusInfo{i}.Inputs = ElementNum;

end

%% 生成Bus模块

if verLessThan(‘simulink‘,‘8.1‘)%2013a之前用mdl,之后用slx

mdlExt = ‘.mdl‘;

else

mdlExt = ‘.slx‘;

end

BusLibFile = char(BusLibName);

if exist([BusLibFile,mdlExt])

delete([BusLibFile,mdlExt]);%删除原先存在的模型

end

close_system(BusLibFile,0);

new_system(BusLibFile,‘Library‘);

load_system(BusLibFile);

for i = 1:BusObjNum

busblock = add_block(‘built-in/BusCreator‘,[BusLibFile,‘/‘,BusInfo{i}.BusName],‘Position‘,[15+(i-1)*115,19,65+(i-1)*115,121]);

set_param(busblock,‘OutDataTypeStr‘,[‘Bus:‘,BusInfo{i}.BusName]);

set_param(busblock,‘Inputs‘,num2str(BusInfo{i}.Inputs));

end

save_system(BusLibFile);

open_system(BusLibFile);

下面是给出来配置Bus的xml代码:xmlbus.xml

1

2

3

4

5

6

7

8

9

10

11

12

以前没咋用过xml,估计写的有很多该规范的地方,欢迎大家指正。

下面是主代码:

xmlfile = ‘xmlbus.xml‘;

xmlbuscreator(xmlfile);

运行上面代码,则可以生成BusLib3.slx文件,里面包含两个Bus如下图所示:

bda65030ec8df5f95e9ab9dd9faa3b02.png

可以看出,生成的两个Bus符合预设的条件,完工。

第一篇记录编程的博客,比较粗糙,发现问题的哥们尽管喷

原文:http://www.cnblogs.com/spyplus/p/5388983.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值