{"id":8718,"date":"2019-03-15T14:47:36","date_gmt":"2019-03-15T19:47:36","guid":{"rendered":"https:\/\/blogs.mathworks.com\/simulink\/?p=8718"},"modified":"2019-03-15T14:47:36","modified_gmt":"2019-03-15T19:47:36","slug":"importing-data-from-excel","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2019\/03\/15\/importing-data-from-excel\/","title":{"rendered":"Importing Data From Excel"},"content":{"rendered":"<p>Today I want to share a useful tip for importing signals from Excel into a simulation.<\/p>\n<p><strong>From Spreadsheet block<\/strong><\/p>\n<p>In case you were not aware, since R2015b, Simulink has offered the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2018b\/simulink\/slref\/fromspreadsheet.html\">From Spreadsheet<\/a> block.<\/p>\n<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2019Q1\/FromSpreadsheetInLib.png\" alt=\"From Spreadsheet block\" \/><\/p>\n<p>Using this block, you can specify an Excel file, sheet name, and range of the data.<\/p>\n<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2019Q1\/FromSpreadsheetDialog.png\" alt=\"From Spreadsheet dialog\" \/><\/p>\n<p><strong><strong>Configuring the From Spreadsheet block<\/strong><\/strong><\/p>\n<p>Let's take a simple example Excel sheet like the following, where we have points sampled at every 0.1 seconds:<\/p>\n<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2019Q1\/fromSpreadsheetExcel.png\" alt=\"Excel sheet\" \/><\/p>\n<p>If I configure the From Spreadsheet block as in the above image, specifying that the first column is time, I will very likely see either duplicate or skipped points in the simulation.<\/p>\n<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2019Q1\/FromSpreadsheetResults.png\" alt=\"Excel sheet results\" \/><\/p>\n<p>Why is that? This is due to the same phenomenon as I explained in this <a href=\"https:\/\/blogs.mathworks.com\/simulink\/2012\/02\/09\/using-discrete-data-as-an-input-to-your-simulink-model\">previous post about importing discrete signal data in Simulink<\/a> <\/p>\n<p>If we compare the \"equally spaced\" time points from the Excel spreadsheet, you will notice that they are slightly different from the equally spaced time points taken by Simulink:<\/p>\n<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2019Q1\/FromSpreadsheetComparison.png\" alt=\"Compare Excel vs Simulink\" \/><\/p>\n<p>In this example, when Simulink takes a point at 13.6sec, it sees that the data contains no point at exactly 13.6sec and holds the previous value from 13.5sec. At 13.7sec, it will output the last point it saw in the data, the one at 13.6000000000099sec.<\/p>\n<p><strong>How can you avoid this situation?<\/strong><\/p>\n<p>To avoid this confusion between the steps taken by Simulink and the steps specified in the Excel sheet, what I recommend is to configure the From Spreadsheet block to ignore the time column in the Excel sheet.<\/p>\n<p>To do that, in the block dialog, set the Sample Time to the appropriate value, then set the <b>Treat first column as<\/b> to <b>Data<\/b> and specify the <strong>Range<\/strong> to start at the second column:<\/p>\n<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2019Q1\/FromSpreadsheetDataMode.png\" alt=\"Compare Excel vs Simulink\" \/><\/p>\n<p>That way, the From Spreadsheet block will simply use one row of the Excel sheet at every time step.<\/p>\n<p><strong>Now it's your turn<\/strong><\/p>\n<p>Are you using the From Spreadsheet block? Did you run in this issue?<\/p>\n<p>Let us know if you have suggestions on how we could improve the From Spreadsheet block to avoid this situation.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2019Q1\/FromSpreadsheetDataMode.png\" onError=\"this.style.display ='none';\" \/><\/div>\n<p>Today I want to share a useful tip for importing signals from Excel into a simulation.<br \/>\nFrom Spreadsheet block<br \/>\nIn case you were not aware, since R2015b, Simulink has offered the From Spreadsheet... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2019\/03\/15\/importing-data-from-excel\/\">read more >><\/a><\/p>\n","protected":false},"author":41,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,33,16],"tags":[562,564],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/8718"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/comments?post=8718"}],"version-history":[{"count":18,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/8718\/revisions"}],"predecessor-version":[{"id":8754,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/8718\/revisions\/8754"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=8718"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=8718"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=8718"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}