Export bus datatype from referenced subsystem

I have created a nested bus in a referenced subsystem in simulink and would like to use the bus datatype again in a different model. For this i would like to export the datatype from simulink to a dataDictionary. I tried to open the referenced subsystem in a model and use the commands Simulink.Bus.createObject and Simulink.Bus.save. But i was not able to generate the object in matlab to save it afterwards. Is there a easier solution to this?

9 Comments

Why not and what is the error or message? If not supported by referenced subsystem, I can think of the last resort is to copy the subsystem to a model. I believe the whole model has to pass model update to create the bus object.
To create the Bus, i first tried to save the bus to a file, since this looks easier to do. So i tried the first three commands of the matlab help "Save Simulink.Bus object definitions in function". I used the following commands:
mdl = "BusHierarchy";
block = "BusHierarchy/Bus Creator1";
load_system(mdl)
busInfo = Simulink.Bus.createObject(mdl,block);
file1 = "DefaultFunction";
Simulink.Bus.save(file1); -> This command lead to an default matlab file without any content of the Bus Creator1
After this, i tried the next command with the same output. Just a default file without any content of the Bus Creator1
Simulink.Bus.save(file2,"object");
After this, i tried the third command, where i got the following error
Simulink.Bus.save(file3,"object",{'NestedBus'});
Error using slbus_get_object_from_name_withDataAccessor
Bus object 'NestedBus' does not exist in the data sources connected to the model.
But this could also be, because the nested busses are not named in my model.
It works, if i create a new simulink model with just some constant and sine inputs to a nested bus. With the above mentioned command "busInfo = Simulink.Bus.createObject(mdl,block);", i get the simulink busses in the workspace. But if i use the same command with my own model, i can use the command and get a struct with the correct bus name, but the simulink bus classes are not created in my workspace.
That is what I mean "the whole model has to pass model update to create the bus object.". Your model has to have the real content for that bus structure and the model has to be error free.
Lukas
Lukas on 10 May 2024
Edited: Lukas on 10 May 2024
Sorry my bad, i did not mention this correctly. My model can be updated and simulated. So it does not have any warnings (I let the all the standard warning checks active, no checks are disabled) nor has it any errors. I get logging Information on the selected bus as well after the simulation of the model in the data inspector. To be able to do this i placed the referenced subsystem in a model and connected the inputs of the referenced subsystem to a signal editor. In this setup im able to log the signals and busses, but i have not jet found a way to store a simulink bus in the workspace. Would this count as real content for the bus structure?
Maybee i did overlooked a simulink design rule?
Now I am lost. You said "It works, if i create a new simulink model with just some constant and sine inputs to a nested bus". Were you able to do it on your real model?
Hy @Fangjun Jiang, thank you for your support in this
Sorry, for the misunderstanding, i checked the command on two different simulink models. First, on a new generic model, that just holds an easy Bus, that consists of a fiew constant and sine inputs. There, I was able to use the matlab commands.
After this, I used the same matlab commands on my model where i wanted to generate the bus structure in the first place. This did not work. I dont have any warnings, if i update or simulate this model. If i use the command "busInfo = Simulink.Bus.createObject(model,block)" , I dont have any warnings or errors either. But this command generated the simulink bus variables in my workspace in my first model. In the second model, it just generated the busInfo variable as a struct with the names of the busses inside, but no bus variables. I would have expected at least 3 to 5 Bus variables in my workspace, if I use this command in my second model.
If i use the command "Simulink.Bus.save("BusStructs","object");" afterwards, i get a file called BusStructs.m that has just 2 lines of code in it:
function BusStructs()
% BUSSTRUCTS initializes a set of bus objects in the MATLAB base workspace
To me, this sounds, as if the command Simulink.Bus.createObject did not work.
My second model is more complicated with arround 6 referenced subsystems in the model. Could it be, that matlab gets a problem, if i use "inherit datatype" on the inputs to my referenced subsystems and build new busses with these inputs?
That is weird. No more advice other than trying on the real model. Maybe contact the Mathworks tech support.
I check again, maybee its just a setting i forgot. If i find my error, i will update the forum.

Sign in to comment.

Answers (1)

Hi Lukas,
In your given code :
mdl = "BusHierarchy";
block = "BusHierarchy/Bus Creator1";
load_system(mdl)
busInfo = Simulink.Bus.createObject(mdl,block);
file1 = "DefaultFunction";
Simulink.Bus.save(file1); -> This command lead to an default matlab file without any content of the Bus Creator1
I am not able to figure out what is your referenced sub-system?
I will share my process and code for your reference:
1. Opening a Bus Object creation example
openExample('simulink/CreateSimulinkBusObjectFromBlockExample')
2. Modifying the BusObjectCreationModel.slx : I selected everything and right clicked to make a Subsystem named SS1
3. Modifying the CreateSimulinkBusObjectFromBlockExample.mlx file:
open_system('BusObjectCreationModel')
%Create a Bus object that corresponds with the bus created by the Bus Creator block.
busInfo = Simulink.Bus.createObject('BusObjectCreationModel','BusObjectCreationModel/SS1/Bus Creator'); %here I have added SS1 in the block path
Simulink.Bus.save('BusObjectFunctionCellFormat'); % here I have added the bus.save function to generate the BusObjectCreationModel.slx
Here is the documentation for the Simulink.Bus.save https://www.mathworks.com/help/simulink/slref/simulink.bus.save.html

Products

Release

R2023a

Asked:

on 7 May 2024

Answered:

on 22 May 2024

Community Treasure Hunt

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

Start Hunting!