使用 readtable
从 Excel 中导入查找表数据
此示例说明如何在模型中使用 readtable
函数将数据导入到查找表中。
提示
您也可以不将数据导入查找表,而是将 Excel® 电子表格的内容复制并粘贴到查找表电子表格中(请参阅Copy and Paste Lookup Table Data from Excel)。
将 Excel 文件保存到 MATLAB® 路径上的文件夹中。
打开包含查找表模块的模型,并选择模型设置 > 模型属性。
在“模型属性”对话框的回调选项卡上,点击模型回调列表中的 PostLoadFcn 回调。
在文本框中输入以下代码,以导入 Excel 电子表格数据。对二维查找表使用 MATLAB 函数
readtable
,如以下示例中所示。% 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);
点击确定。
当您保存更改之后,下次打开该模型时,Simulink® 将调用该回调并导入数据。