Bus signal: save and load
조회 수: 22 (최근 30일)
I have a simulink model from which I want to save a bus signal and load this into another simulink model. I have no problems with saving the bus signal (I think) but I can't get the bus signal loaded in an other file. I have made a simple example (see attachment) to get an idea what I want to do. test_bus_save saves the signal :) and I want to load that signal in test_bus_load.
What am I doing wrong? Is it possible to save a bus signal and load it into another model?
In my example I want to put the signal to workspace but in reality I just want to use the data from the signal in another model. But the problems is with loading the bus signal.
I've used Matlab 2016b and 2018b (the files are created with 2018b)
Sara Nadeau 2019년 6월 27일
When I try the attached models, I get an error about the Output data type setting for the From File block. To use this method for logging/loading bus data, you'll need a Simulink.Bus object to use as a data type specification for the From File block. Then, you can set the From File block Output data type to Bus: myBus and load the data.
If you have access to R2019a, you can load bus data without needing a Simulink.Bus object using In Bus Element blocks.
Sara Nadeau 2019년 6월 28일
You're doing just the right thing with logging the bus data. That piece tends to be a bit easier than loading bus data in Simulink.
With any of the loading blocks, including From File and Inport blocks, you need to tell the block what kind of data to expect. Because buses are flexible, in order to load bus data, you need to have a specification for what the loading block should expect to find in the bus you're loading.
Luckily, there are some helper functions to make creating this bus object easy, when you want to load bus data that was logged from another model. You can use the Simulink.Bus.createObject function to create a Simulink.Bus specification for the bus you log in test_bus_load.
busInfo = Simulink.Bus.createObject('test_bus_save','test_bus_save/Bus Creator');
The return is a structure array (scalar, in this case, because we only passed in one block name) with a field for the block handle (block) and the name of the Simulink.Bus object specification (busName). The default name used when you do not name the bus signal in the model is slBus1, and if you name the bus signal, the Simulink.Bus specification created using this function takes the same name.
You can save this Simulink.Bus specification to a file and load the specification into the base workspace using a model callback for the model that loads the bus. Whatever strategy you use, the model loading the bus data needs access to the corresponding Simulink.Bus object.
When you specify the Output data type for the From File block, use the name of the Simulink.Bus object, not the name of the variable containing the structure of bus data. In this case, you would enter Bus: slBus1.
If you look into the bus object created with the Simulink.Bus.createObject function, you'll see it contains Simulink.BusElement objects that specify properties for each bus element, including the name and data type.
I hope this helps!
Sara Nadeau 2019년 7월 2일
No problem - I've attached a modified version of your example model that loads bus data showing how to create the Simulink.Bus object and use it to define the data type for the From File block. I added code and explanations of the necessary steps in the PreLoadFcn model callback. To get to the model callbacks, click the Model Explorer button (just to the right of the Model Configuration Paramters (gear) button). In the right-most section of the Model Explorer, click the Callbacks tab, and then you should see where I added the PreLoadFcn callback. When you load the model, it executes the code here.
I explained two different strategies for creating the Simulink.Bus object, depending on whether you're OK loading the structure of logged bus data into the workspace. The strategy that does not load data into the workspace requires you to run a line of code in the Command Window before opening the model and causing it to execute the callback. The code for that method is commented out, so the 'active' strategy in the callback loads the data from the file into the workspace and creates the Simulink.Bus object based on the structure of logged bus data. I built this example assuming that you had already run the model that logs the bus data, so the mat-file already exists on the path.
If you're OK loading data into the workspace, you can also use a root-level Inport block to load the bus data. I added one to the model as an example, in case it would be helpful for you.
I think this topic might help explain the concept of Simulink.Bus objects a bit more, though it does not specifically mention the use case we're working with, which is loading bus data as an external input. Basically, the Simulink.Bus object describes the 'shape' of the data, and the structure provides the data itself.
I hope this helps. Please let me know if you have any issues or if something does not work for you.