Best way to save bus definition for Simulink Library

Hi all,
I have a Simulink library file which uses some bus definitions, and I have an .m file which creates such buses. Currently the script creates the buses in the base workspace using Simulink.Bus.cellToObject .
I want change this so that it does not use the base workspace anymore. What is the suggested option to provide the bus to whatever model uses the library blocks?
Thank you,
Marco

 Accepted Answer

To provide the bus to models that use the library blocks without relying on the base workspace, you can consider using Simulink data dictionaries. Data dictionaries allow you to store and manage data definitions, including bus objects, separately from the models.
Here's a suggested approach:
1. Create a new data dictionary in Simulink by going to the "File" menu, selecting "New", and then choosing "Data Dictionary".
2. Open the data dictionary and define your bus objects using the "Bus Editor" tool. You can create the buses manually or import them from your existing .m file.
3. Save the data dictionary.
4. In your library file, replace the code that creates the buses in the base workspace with code that retrieves the buses from the data dictionary.
- First, load the data dictionary using the `Simulink.data.dictionary.open` function.
- Then, use the `getBusObject` function to retrieve the bus object from the data dictionary.
Here's an example:
% Load the data dictionary
dictionary = Simulink.data.dictionary.open('path/to/your/dictionary.sldd');
% Retrieve the bus object from the data dictionary
busObject = getBusObject(dictionary, 'YourBusObjectName');
5. Update your library blocks to use the `busObject` instead of accessing the bus directly from the base workspace.
By using data dictionaries, you can ensure that the bus definitions are centralized and can be accessed by any model that references the data dictionary. This approach enhances modularity and makes it easier to manage and update the bus definitions across multiple models.

5 Comments

Hi Nandini,
thank you for the detailed answer.
Some points:
  1. I want to keep my bus specification in an ASCII source file (for versioning, searching etc.). Is there an option to specify the data dictionary as code?
  2. you say "In your library file, replace the code that creates the buses in the base workspace with code that retrieves the buses from the data dictionary". How can I do this? I currently have an .m file to create the buses, but this is not tied to the library file: I need to run the this file before using the library. I would like something that is triggered automatically whenever the library is used.
  3. Following your example: where should this code go, and where will busObject reside, if not in the base workspace?
Marco
Hi Marco,
Thank you for your follow-up questions. I'll address each of your points below:
  1. Specifying the data dictionary as code: You can programmatically create and define the bus objects within a data dictionary using MATLAB code. This allows you to keep your bus specification in an ASCII source file and generate the data dictionary programmatically using that file.
  2. Automatically triggering the bus creation: Use the Initialization Callback feature in Simulink. Define an initialization function in your library that gets executed when a model containing the library is loaded. Inside this function, call your .m file that creates the buses. This ensures that the buses are created automatically whenever a model using the library is opened.
  3. Location of busObject outside the base workspace: Instead of storing the busObject in the base workspace, store it as a parameter or a constant block within your library. This makes the busObject available within the library and accessible by the blocks that require it.
Example code structure:
function initializeLibrary
% Call your .m file to create the buses
createBuses();
end
Define the initializeLibrary function as the Initialization Callback for your library. This function will be automatically triggered whenever a model using the library is loaded, ensuring that the buses are created.
Feel free to let me know if you have any further questions or need additional clarification!
Hi again Nandini,
thank you, this helps a lot ideed. Concerning your third point "Instead of storing the busObject in the base workspace, store it as a parameter or a constant block within your library": could you elaborate on how this is done? By parameter, do you mean "block-specific parameters"? And by constant block, do you mean a simulink constant block? Could this output a bust definition, rather than a bus value?
Marco
Hi Marco,
Let me elaborate on storing the busObject within your library using parameters or constant blocks:
1. Block-specific parameters: In Simulink, you can define block-specific parameters for your library blocks. These parameters can be used to store the busObject. Here's how you can do it:
- Open your library and select the block that requires the busObject.
- In the block's parameter dialog, create a new parameter (e.g., "BusObjectParam").
- Set the value of "BusObjectParam" to the busObject retrieved from the data dictionary in your library's initialization function.
- Save the library.
2. Simulink Constant Block: Another option is to use a Simulink Constant block to store the busObject. This block can output a constant value, including a bus definition. Here's how you can do it:
- In your library, add a Simulink Constant block.
- Open the block's parameter dialog and set the value of the constant to the busObject retrieved from the data dictionary in your library's initialization function.
- Connect the output of the Constant block to the blocks that require the busObject.
- Save the library.
By using block-specific parameters or Simulink Constant blocks, you can store the busObject within your library and make it available to the blocks that require it. This way, the bus definition is accessible within the library itself, instead of relying on the base workspace.
I hope this clarifies the process.
Hi Nandini,
thank you again for your reply. I have tested your suggestions, I am now unable to work out all the details, but I plan to come back to them at some point.
Meanwhile, thank you for all the details and the pointers, it has helped me a lot.
Marco

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2019b

Community Treasure Hunt

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

Start Hunting!