How to programmatically access (and edit) bus elements
Show older comments
My project has a restriction that bus elements need to be all lowercase. The architecture has bus objects stored in data dictionaries, and the model uses these in BusCreator objects.
In order to enforce compliance with this restriction, I'd like to programmatically inspect all the buses used in a model, and/or bus objects stored in the data dictionaries. When an offending element is found, I might
- just print an error message,
- provide a dialog to help the user edit the name, or
- do some kind of automatic name change.
But the first issue is to identify the uppercase and camelcase bus elements.
So far, here's what has not worked for me:
1. Inspecting the BusCreator
I've tried selecting a BusCreator block which has elements that need to be corrected, then using the command
get_param(gcb,'DialogParameters')
but the result doesn't contain the bus name, much less the element names.
2. Finding Bus Definitions used by the model
This gets me a little closer; I can use
modelVariables = Simulink.findVars(bdroot,'SourceType','data dictionary')
then check each of the modelVariables.Users to see if that user block is a BusCreator; in which case the variable is probably the name of a bus object. But once I have the bus names, I still need to find the element names, and I'm stumped there.
3. Finding Bus Definitions in the SLDD
I've tried using the Simulink.data.Dictionary class methods to find bus objects' elements names, but haven't found a way to actually find the bus element names, much less edit them.
4. Model Advisor Checks
I took a brief look, but didn't see a way to customize mathworks.maab.jc_0221 Check character usage in signal labels to flag uppercase characters. I don't necessarily want to exclude uppercase on ALL signals, although doing that would probably end up helping achieve all lowercase bus elements.
Accepted Answer
More Answers (2)
Walter Roberson
on 23 Dec 2016
Playing around a bit, and looking at the Block Specific Parameters documentation of get_param, I see that Bus Selector blocks have a property InputSignals which are the names of the signals on the bus.
I also see that get_param() of 'OutputSignalNames' on a Bus Creator object gives the bus name.
However at the moment I do not know how to find all the Bus Creator or Bus Selector objects.
Ah...
bus_creator_info = find_system(bdroot, 'BlockType','BusCreator');
num_bus = length(bus_creator_info);
bus_name = cell(num_bus, 1);
for K = 1 : num_bus
bus_name{K} = get_param(bus_creator_info{K}, 'OutputSignalNames');
end
bus_selector_info = find_system(bdroot, 'BlockType','BusSelector');
num_bus = length(bus_selector_info);
bus_name = cell(num_bus, 1);
for K = 1 : num_bus
bus_signals{K} = get_param(bus_selector_info{K}, 'InputSignals');
end
You could clearly adapt this to look for particular characters in the signal names.
2 Comments
John Harris
on 23 Dec 2016
Edited: John Harris
on 23 Dec 2016
Alireza Tajafari Sahebi
on 11 May 2022
Edited: Alireza Tajafari Sahebi
on 11 May 2022
To get the bus elements name:
get_param (enter your bus name here, 'PortConnectivity')
After runing, you will have a structure in your workspace. This structure has 6 fields. One of these fields is SrcBlock.
SrcBlock includs all handles of all input signals of your bus creator.
Hi John,
"3. Finding Bus Definitions in the SLDD
I've tried using the Simulink.data.Dictionary class methods to find bus objects' elements names, but haven't found a way to actually find the bus element names, much less edit them"
I managed to do it with Simulink.data.Dictionary class method, may be useful:
Assumption: data dictionary with name 'myDictionary.sldd' exists and contains a bus object named 'mybus1'
%% Open the data dictionary (ssuming a data dictionary is already created) and move the newly created bus object to the data dictionray
myDictionaryObj = Simulink.data.dictionary.open('myDictionary.sldd'); % open the data dictionary
dDataSectObj = getSection(myDictionaryObj,'Design Data'); % access the Design Data section
%% Get the bus object details and assign the dimension variable to Dimensions filed
busobject_details = getEntry(dDataSectObj,'mybus1'); % get bus object details
bus_objval=getValue(busobject_details); % get the values of bus object

element_size=length(bus_objval.Elements);
elem_names=strings(1,element_size); % empty string array to hold the element names
for i=1:element_size
elem_names(1,i)=convertCharsToStrings(bus_objval.Elements(i).Name);% get the names of elements
end
%% Carryout your customization based on the found element names
%dummy code
% edit and update the bus element names to data dictionary
new_names = ["new_name1" "new_name2"]; % 'mybus1' has 2 elements and both elements name to be changed, this array can vary depending upon the size of elements whose name to be changed
for j=1:element_size % here instead of element size, size of elements whose names should be changed should be assigned
bus_objval.Elements(j).Name=new_names(j); % assign new name to element 1
%bus_objval.Elements(2).Name='new_name2';% assign new name to element 2
end
%Note: A for loop can be defined to pass the new names of elements from an
%array
setValue(busobject_details,bus_objval); % update the data dictionary entry
Categories
Find more on Manage Design Data in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!