Main Content

Import Lookup Table Data from Excel Using readtable

This example shows how to use the readtable function in a model to import data into a lookup table.

Tip

Instead of importing data into a lookup table, you can also copy and paste the contents of an Excel® spreadsheet into a Lookup Table spreadsheet (see Copy and Paste Lookup Table Data from Excel).

  1. Save the Excel file in a folder on the MATLAB® path.

  2. Open the model containing the lookup table block and in the Modeling tab, select Model Settings.

  3. In the Model Properties dialog box, in the Callbacks tab, click PostLoadFcn callback in the model callbacks list.

  4. Enter the code to import the Excel Spreadsheet data in the text box. Use the MATLAB readtable function, as shown in this example for a 2-D lookup table.

    % Import the data from Excel for a lookup table
    data = readtable('MySpreadsheet','Sheet',Sheet1);
    % Row indices for lookup table
    breakpoints1 = data(2:end,1)';
    % Column indices for lookup table
    breakpoints2 = data(1,2:end);
    % Output values for lookup table
    table_data = data(2:end,2:end);
    

  5. Click OK.

After you save your changes, the next time you open the model, Simulink® invokes the callback and imports the data.

Related Topics