Module 8: Bridging Simulink and the MATLAB Environment
8.1. Exporting Simulation Data to the MATLAB Workspace
A critical part of the engineering workflow is the post-processing and detailed analysis of simulation data. While the ‘Scope’ block is excellent for real-time visualization, Simulink provides a straightforward method for logging signal data directly to the MATLAB workspace, where it can be manipulated, plotted, and analyzed using MATLAB’s extensive set of functions.
We will use a simple sine wave model to demonstrate this process.
- Create the Model: Build a basic model with a ‘Sine Wave’ block connected to a ‘Scope’.
- Configure Logging:
- Navigate to the model’s configuration properties (often found under the “Simulation” tab or by clicking a gear icon).
- Select the ‘Data Import/Export’ tab (often labeled ‘Logging’ in older versions).
- Check the box labeled “Log data to workspace”.
- Set Logging Parameters:
- In the options that appear, you can specify a ‘Variable name’. Let’s set this to swave.
- You can also specify the ‘Save format’. A common and useful choice is ‘Array’.
- Apply these changes.
- Run and Observe: Run the simulation again. This time, after the simulation completes, observe the MATLAB workspace. You will see a new variable has been created, named out by default, which contains our logged data.
- Access the Data: In the MATLAB command window, you can now interact with this data. To display the raw data array, you can type:
- This will print the time-series data for the sine wave.
- Plot in MATLAB: To demonstrate the power of this integration, you can use MATLAB’s plotting capabilities on the exported data. Enter the following command:
- MATLAB will generate a new figure window displaying a graph of the sine wave, which should perfectly match the output you saw in the Simulink ‘Scope’.
8.2. Automating Model Creation with MATLAB Scripts
While direct graphical manipulation is the most common way to build models, Simulink’s Application Programming Interfaces (APIs) allow you to programmatically create, configure, and connect blocks using MATLAB scripts. This is essential for automating repetitive tasks, creating very large or complex models, and integrating Simulink into larger automated workflows.
We will now examine a script that programmatically creates our simple sine wave model.
- Create a new model file: This command creates a new, blank Simulink model file named ‘matlabmodel.slx’.
- Open the model window: This command opens the newly created model’s graphical editor.
- Add the Sine Wave block: The add_block command requires two primary string arguments: add_block(‘source_block_path’, ‘destination_path_in_model’). The source specifies the block from the library, and the destination specifies the path within our model and the name for the new block (‘Sine’).
- Add the Scope block: We add the Scope block in the same way, but with an optional name-value pair to control its position. The vector [left, top, right, bottom] defines the block’s pixel coordinates on the canvas.
- Connect the blocks: The add_line command connects the blocks. The ‘Sine/1’ and ‘Scope/1’ strings specify the connection points. The syntax is ‘block_name/port_number’, where /1 denotes the first output or input port of the respective block.
With the model now fully constructed via script, we can also run the simulation and view the results programmatically.
- Run the simulation: The sim command executes the model and returns the results to a variable.
- View the output: This command opens the Scope window within our model to display the simulation results.
This module has highlighted the powerful two-way integration between Simulink and MATLAB. Data can flow from a simulation into MATLAB for analysis, and model creation itself can be driven from MATLAB scripts for automation. With this comprehensive set of skills, it is time to apply them to solve more formal mathematical problems.