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!

Answers (1)

Fangjun Jiang
Fangjun Jiang on 15 Feb 2022
Edited: Fangjun Jiang on 15 Feb 2022
Try it on one block, get and set the data type
get_param('ModelName/InportName','OutDataTypeStr')
set_param('ModelName/InportName','OutDataTypeStr', 'Bus: xxxx')

10 Comments

Great! I am trying to set the Data Type to the port name (Bus: "Port Name"). Is there a way to pull the Port Name from the Command Window? I can't seem to figure this part out. Thanks again!
get_param('ModelName/InportName','Name')
Unfortunately that provides the Block name, not the Bus Port name. For example, if the block is a Bus Element In, it could be something like "inputBus.t.inletTt", I would want to pull "inputBus" programatically, and then set the data type to "Bus: inputBus".
The command you provided returns "BusElementIn150" (where 150 could be any number of course), which is the name of the Block, not the name of that block's Bus Port.
I see. This is for "In Bus Element" block.
get_param('ModelName/InportName','PortName')
Hi, coming back to this for followup questions. The code you had suggested works, sort of. What I am trying to do is change the "Data type" for the entire bus, but your code changes the "Data type" for the signal. I have attached images to aid in my goal. The first image is what you have suggested:
The "Data type" of the signal has been changed to 'Bus: InBus1'.
What I am trying to do, though, is to change the "Data type" of the entire Bus, from 'Inherit: auto' to 'Bus: InBus1', like this:
Notice that the bus name (Inbus1) and the signals (signal1 and signal2) are now red and italicized.
Is this possible using code in the Command Window?
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".
How could I go about doing it at the Subsystem level? For example, the bus is sent into a subsystem from a GoTo tag into an inport of the subsystem.
The steps and codes still apply but doing that is a bad modeling practice.
Could you please explain why it would be a bad modeling practice? Our system has many busses, all of which are routed as From/GoTo's at a particular level of the model.
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.

Sign in to comment.

Categories

Products

Release

R2021b

Asked:

on 14 Feb 2022

Edited:

on 28 Apr 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!