Move Data Between MATLAB and Simulink Model

Import Data from the MATLAB Workspace

You can import data from the MATLAB® workspace to the Simulink® model input ports. This allows you to import actual physical data into your model. For information about other data import capabilities, see Import Data.

In this example, you create a vector of temperature data in MATLAB and use that data as an input to the Simulink model.

To import data from the MATLAB workspace:

  1. In the MATLAB Command Window, create time and temperature data by entering the following commands:

    x = (0:0.01:4*pi)';
    y = 32 + (5*sin(x));
    z = linspace(0,48,1257)';

    y is a vector of outdoor temperatures that varies from 27 to 37 degrees Fahrenheit. z is a time vector from 0 to 48. Both vectors contain 1257 values.

  2. In the Simulink Editor model window, select the Avg Outdoor Temp block, and then press the Delete key to remove it from the model.

  3. Delete the following items from the model in the same way:

    • Daily Temp Variation block

    • Two input signal lines to the Sum1 block

    • Sum1 block

    The model should now look similar to the following figure. Notice that the output signal from the Sum1 block changes to a red, dotted line, indicating that it is not connected to a block.

  4. In the model window, select View > Library Browser. The Simulink Library Browser window opens.

  5. In the Library Browser, in the Libraries pane, select the Sources library.

  6. From the Sources library in the right pane, select the In1 block, and then drag it to the Simulink Editor model window.

    An instance of the In1 block appears in the model window.

  7. Connect the dotted line (originally connected to the Sum1 block) to the In1 block.

  8. In the Simulink Editor, select Simulation > Model Configuration Parameters.

  9. From the menu on the left side of the Configuration Parameters dialog box, select Data Import/Export.

  10. On the Data Import/Export pane, select the Input check box. In the Input field, enter [z,y].

  11. Click OK. The software applies your changes and closes the dialog box.

  12. Rerun the simulation.

    The software simulates the model. Notice that the model ran using the imported data, showing colder outdoor temperatures and higher heat use.

Export Simulation Data to the MATLAB Workspace

Once you complete a model, you may want to export your simulation results to the MATLAB workspace for further data analysis or visualization. For information about additional data export capabilities, see Export Runtime Information.

To export the HeatCost data from the model to the MATLAB workspace:

  1. In the Simulink Library Browser, select the Sinks library.

  2. From the Sinks library, select the Out1 block, and then drag it to the top right of the Simulink Editor model window.

    An instance of the Out1 block appears in the model window.

  3. Draw a branch line from the HeatCost signal line to the Out1 block.

  4. Select Simulation > Model Configuration Parameters.

  5. From the left side of the Configuration Parameters dialog box, select Data Import/Export.

  6. On the Data Import/Export pane, select the Time and Output check boxes.

  7. Click OK. The software applies your changes and closes the dialog box.

  8. Rerun the simulation.

    The software simulates the model and saves the time and HeatCost data to the MATLAB workspace in the variables tout and yout.

Was this topic helpful?