Assign Port name "Data Type" from Command Window
显示 更早的评论
Hello, I am hoping to get some help with writing a script to expedite some manual bus port setting changes I have to make occasionally in Simulink. Is it possible to change a Bus's Data Type from the Command Window? If so, where would I find this command (or better yet, what is the command I would use)?
I've attached a screenshot to show what I mean. Thanks in advance!

回答(1 个)
Fangjun Jiang
2022-2-15
编辑:Fangjun Jiang
2022-2-15
1 个投票
Try it on one block, get and set the data type
get_param('ModelName/InportName','OutDataTypeStr')
set_param('ModelName/InportName','OutDataTypeStr', 'Bus: xxxx')
10 个评论
Cory Dinkle
2022-2-17
Fangjun Jiang
2022-2-17
get_param('ModelName/InportName','Name')
Cory Dinkle
2022-2-17
编辑:Cory Dinkle
2022-4-22
Fangjun Jiang
2022-2-18
I see. This is for "In Bus Element" block.
get_param('ModelName/InportName','PortName')
Cory Dinkle
2022-4-25
编辑:Cory Dinkle
2022-4-25
Fangjun Jiang
2022-4-26
编辑:Fangjun Jiang
2022-4-26
To change the "Data Type" property value of a "Bus Element In" block from "Inherit: auto" to "Bus: Inbus", is to specify this "Bus Element In" block as the source of the bus. This should be done only at the root level of the model.
So, at the root level of the model,
- Add a "Bus Element In" block, double click it
- Select "InBus", click icon "Add block for selected signals"
- Another "Bus Element In" block shown as "InBus" will be added, select that block
- Run set_param(gcb,'OutDataTypeStr', 'Bus: Inbus1') and it's done
"the bus name (Inbus1) and the signals (signal1 and signal2) are now red and italicized." is because you don't have the bus definition in base workspace. Do the following to resolve it
Inbus1=Simulink.Bus
open Inbus1
then use the GUI to create "signal1" and "signal2".
Cory Dinkle
2022-4-27
Fangjun Jiang
2022-4-27
The steps and codes still apply but doing that is a bad modeling practice.
Cory Dinkle
2022-4-27
Fangjun Jiang
2022-4-28
编辑:Fangjun Jiang
2022-4-28
Using Goto/From block should be no reason/jusitificaiton for doing this. In principle, a signal or bus should be defined only once, preferably at its true source (e.g. a Bus Creator block). All other places like Ports they should be inherited. Once the model is updated, you will see the propagated bus name/definition. Inport block at the root level is regarded as source.
Defining the same bus in multiple places could cause inefficiency in code generation, even when the definition is consistent. When you need to make a change, you have to make the same change in multiple places. When the definition is not consistent, you got errors.
类别
在 帮助中心 和 File Exchange 中查找有关 Interactive Model Editing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



