This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Save and Import Bus Objects

When you create bus objects in the base workspace, before you close MATLAB®, save (export) the bus objects to one of these locations:

  • Data dictionary

  • MATLAB code file

  • MAT-file

  • Database or other external files

If you do not save the bus objects, then when you reopen the model that uses the bus objects, you need to recreate the bus objects.

The technique you use for creating a bus object determines where the bus object is stored initially.

Bus Creation TechniqueInitial Storage Location
Bus EditorBase workspace
Simulink.Bus and Simulink.BusElement object definitionBase workspace
Simulink.Bus.cellToObjectBase workspace
Simulink.Bus.createObjectBase workspace or MATLAB code file

When you save bus objects using the Bus Editor, you can save them in a MAT-file or MATLAB code file. When you save bus objects using the Simulink.Bus.save or matlab.io.saveVariablesToScript function, the bus objects in the base workspace are saved in a MATLAB code file in object form. You can use any MATLAB technique that saves the contents of the base workspace. However, the resulting file contains everything in the base workspace, not just bus objects.

Tip

You can configure the Bus Editor so that closing it posts a reminder to save bus objects. To enable the reminder, select Options > Always Warn Before Closing. When you select this option and try to close the Bus Editor, a reminder appears about saving bus objects before closing. You can disable the reminder by clearing Options > Always Warn Before Closing.

You can customize bus object export and import by providing a custom function that writes to or reads from a location outside MATLAB. For example, the exported bus objects could be saved as records in a corporate database. See Customize Bus Object Import and Export for details.

When you import and modify bus objects, resave them and possibly modify the mechanism (such as a model callback) that you use for importing them.

Locations for Saving Bus Objects

Before you choose where to save bus objects, consider how you want to associate bus objects with models. For more information, see Map Bus Objects to Models.

LocationUsage Considerations

Data dictionary

Use for large model componentization.

When you save to a data dictionary from the base workspace, you get all the variables used by the model, not just the bus objects.

Before you save to a data dictionary, read Considerations before Migrating to Data Dictionary.

MATLAB code file

Use for when you want to use MATLAB for traceability and model differencing.

MAT-file

Use for faster bus object saving and loading.

Database or other external files

Use for comparing bus interface information with design documents stored in an external data source.

Data Dictionary

Save Bus Objects

To save bus objects (and other base workspace variables used by the model) to a data dictionary:

  1. Link the model to a data dictionary, using the Model Properties dialog box.

  2. Create a data dictionary.

  3. Migrate data from the base workspace to the data dictionary.

For an example showing the complete procedure, see Migrate Single Model to Use Dictionary.

MATLAB Code File

Save Bus Objects

To use the Bus Editor to export all bus objects from the base workspace to a MATLAB code file:

  1. In the Bus Editor, choose File > Export to File.

  2. In the Export dialog box, specify the name for the export file. You do not have to specify a file extension.

  3. Set Save as type to MATLAB files (*.m).

  4. Click Save.

  5. In the dialog box that appears, select the format:

    • Cell — Stores the bus objects in a compressed format.

    • Object — Store the bus objects in Simulink.Bus object format, which is easier to read and edit.

All the bus objects that are in the base workspace, and nothing else, are exported to the specified MATLAB code file.

Note

Operations performed on the Base Workspace in the Hierarchy pane, such as exporting bus objects, affect only visible objects. An object that is invisible because a filter is being used is unaffected by the operation. To export all existing bus objects, before performing the export, clear any filter that is in use.

To export only selected bus objects from the base workspace to a file:

  1. In the Bus Editor Contents pane, select one or more bus objects and right-click.

  2. To export only the selected bus objects, in the context menu, select Export to File. To include nested bus objects used by the selected objects, select Export with Dependent Bus Objects to File.

  3. Use the Export dialog box to export the selected bus objects.

When you create bus objects using Simulink.Bus.createObject, you can specify a MATLAB code file to store the bus objects. If you store the bus objects in a file, by default the objects are stored in cell format, which is a compressed format. To store the objects in a more readable format, use 'object' as the last argument, after the file name. For example:

Simulink.Bus.createObject('busdemo','busdemo/Bus Creator2',...
'bus_objs','object'),

Import Bus Objects

You can use a model callback, using the load function, to load the MATLAB code file.

If a model uses only a few bus objects, consider copying the bus object code directly into the callback, instead of loading a file. For an example, open the ex_bus_tutorial_nonvirtual.slx model and examine the callback.

You can use the Bus Editor to import the definitions from a MAT-file to the base workspace. The import loads the complete contents of the file, not just the bus objects.

  1. Choose File > Import into Base Workspace.

  2. Use the Open File dialog box to navigate to and import the desired file.

MAT-File

Save Bus Objects

To export all bus objects from the base workspace to a MAT-file using the Bus Editor:

  1. In the Bus Editor, choose File > Export to File.

  2. In the Export dialog box, specify the name for the export file. You do not have to specify a file extension.

  3. Set Save as type to MAT—files (*.mat).

  4. Click Save.

All bus objects in the base workspace, and nothing else, are exported to the specified MAT-file.

Note

Operations performed on the Base Workspace in the Hierarchy pane, such as exporting bus objects, affect only visible objects. An object that is invisible because a filter is in use is unaffected by the operation. To export all existing bus objects, before performing the export, clear any filter that is in effect.

To export only selected bus objects from the base workspace to a file:

  1. In the Bus Editor Contents pane, select one or more bus objects and right-click.

  2. To export only the selected bus objects, in the context menu select Export to File. To export any nested bus objects used by the selected objects, select Export with Dependent Bus Objects to File.

  3. Use the Export dialog box to export the selected bus objects.

When you create bus objects using Simulink.Bus.createObject or Simulink.Bus.cellToObject, you can save the bus objects as a MATLAB code file. When you use the Simulink.saveVars function to save variables from the base workspace, the objects are saved in a MATLAB code file.

Import Bus Objects

You can use a model callback, using the load function to load the MAT-file.

You can use the Bus Editor to import the definitions from a MAT-file to the base workspace. Importing the file loads the complete contents of the file, not just the bus objects.

  1. Choose File > Import into Base Workspace.

  2. Use the Open File dialog box to navigate to and import the desired file.

Database or Other External Files

Save Bus Objects

You can capture bus interface information in a database or other external source, and use scripts and Database Toolbox™ functionality to read that information into MATLAB.

Import Bus Objects

You can use sl_customization.m to customize the Bus Editor to import bus data from a database or other external source. For details, see Customize Bus Object Import and Export.

Create Bus Objects from External C Code

You can create a bus object that corresponds to a structure type (struct) that your existing C code defines. Then, in preparation for integrating existing algorithmic C code for simulation (for example, by using the Legacy Code Tool), you can use the bus object to package signal or parameter data according to the structure type. To create the object, use the Simulink.importExternalCTypes function.

See Also

Functions

Classes

Related Topics