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).
Create or open an existing Excel spreadsheet, for example, an Excel spreadsheet that contains this data.
1 25 -7 2 35 -4 4 42 Save the Excel file in a folder on the MATLAB® path, for example
MySpreadsheet.xlsx
.Create a model and add a 2-D Lookup Table block.
In this model, select Model Settings > Model Properties.
In the Model Properties dialog box, in the Callbacks tab, click PostLoadFcn callback in the model callbacks list.
Enter the code to import the Excel spreadsheet data from
Sheet1
in the text box. Use the MATLABreadtable
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};
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.