Main Content

Import Lookup Table Data from Excel Using readtable

This workflow outlines 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. Create or open an existing Excel spreadsheet, for example, an Excel spreadsheet that contains this data.

     125
    -7235
    -4442
  2. Save the Excel file in a folder on the MATLAB® path, for example MySpreadsheet.xlsx.

  3. Create a model and add a 2-D Lookup Table block.

  4. In this model, select Model Settings > Model Properties.

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

  6. Enter the code to import the Excel spreadsheet data from Sheet1 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};

  7. Click OK.

After you save your changes, the next time you open the model, Simulink® invokes the callback and imports the data. To see the imported data, click the View diagnostics link.

Related Topics