{"id":9044,"date":"2019-08-06T14:34:42","date_gmt":"2019-08-06T19:34:42","guid":{"rendered":"https:\/\/blogs.mathworks.com\/simulink\/?p=9044"},"modified":"2019-08-06T14:34:42","modified_gmt":"2019-08-06T19:34:42","slug":"loading-signals-in-timetable-format","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2019\/08\/06\/loading-signals-in-timetable-format\/","title":{"rendered":"Loading Signals in Timetable Format"},"content":{"rendered":"<p>Today I decided to revisit a topic I covered a few years ago: <a href=\"https:\/\/blogs.mathworks.com\/simulink\/2012\/02\/09\/using-discrete-data-as-an-input-to-your-simulink-model\">loading discrete signals in a simulation.<\/a><\/p>\n<p>Let's see what has been added in the last few years to help with the potential problems you might run into when loading discrete signals in a simulation.<\/p>\n<p><strong>The Problem<\/strong><\/p>\n<p>Let's start with a simple Excel sheet with two columns, the first one for time and the second for the signal values. I made the data identical to the time just to make the issue more obvious. The actual file we will use contains 10,000 rows, for a stop time of 10 seconds.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2019Q2\/timetableExcelSheet.png\" alt=\"Excel Sheet\" \/><\/p>\n<p>As I explained in <a href=\"https:\/\/blogs.mathworks.com\/simulink\/2019\/03\/15\/importing-data-from-excel\">this post<\/a>, it is possible to use the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2018b\/simulink\/slref\/fromspreadsheet.html\">From Spreadsheet<\/a> block to load this signal.<\/p>\n<p>If you do so, I strongly recommend to be careful and <strong>NOT<\/strong> load the time column from Excel. Even though the time points in Excel might look equally spaced, they might not be equally spaced in the same way as Simulink time steps. If there is a discrepancy, you might end up with duplicated or missing points.<\/p>\n<p>For the file shown above, here is what the output looks like if you use the default settings in the From Spreadsheet block. The spikes on the bottom axis are indications that points are missed or duplicated.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2019Q2\/timetableFromSpreadsheet.png\" alt=\"Excel Sheet\" \/><\/p>\n<p>See <a href=\"https:\/\/blogs.mathworks.com\/simulink\/2019\/03\/15\/importing-data-from-excel\">this post<\/a> for more details on how to configure the From Spreadsheet block to avoid this issue.<\/p>\n<p>For today's example, I am adding an additional level of complexity:<\/p>\n<ul>\n<li>I want to load the signal through a root-level Inport block<\/li>\n<li>I want the signal to be part of an array of buses<\/li>\n<li>I want the model to have multiple root-level Inport blocks at different rates<\/li>\n<\/ul>\n<p>Using the following model, we want to load the signal from the Excel sheet and assign it to element \"a.a1\" of Inport \"u1\":<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2019Q2\/timetableStartModel.png\" alt=\"Example model\" \/><\/p>\n<p><strong>createInputDataset<\/strong><\/p>\n<p>When you have a model like that for which you want to load signals through root-level Inport blocks, your best place to start is the function <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2019a\/simulink\/slref\/createinputdataset.html\">createInputDataset<\/a>.<\/p>\n<p>This function will analyze your model and create a skeleton <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2019a\/simulink\/slref\/simulink.simulationdata.dataset-class.html\">dataset object<\/a> that fits your model.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2019Q2\/timetableCreateInputDataset.png\" alt=\"Create Input dataset\" \/><\/p>\n<p>In this dataset, each signal is a MATLAB <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/data_analysis\/time-series-objects.html\">timeseries<\/a> with one point at the simulation start time and one point at the stop time, both with a value of zero. All we need to do now is replace those with our real data.<\/p>\n<p><strong>Timetable<\/strong><\/p>\n<p>At this point, we could read the Excel file and create a time series with the first column as time and the second column as data:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2019Q2\/timetableFillDataset1.png\" alt=\"Creating a timeseries\" \/><\/p>\n<p>However, we end up with the same problem as above... since the time column in Excel is not identical to the steps taken by Simulink, we still see a few missed points:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2019Q2\/timetableFillDatasetResult1.png\" alt=\"Wrong results\" \/><\/p>\n<p>Fortunately, instead of a timeseries, it is possible to fill the dataset with a <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2019a\/matlab\/ref\/timetable.html\">timetable<\/a>. Instead of <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2019a\/matlab\/ref\/xlsread.html\">xlsread<\/a>, it is possible to read the data directly using <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2019a\/matlab\/ref\/readtimetable.html\">readtimetable<\/a>, which is more modern and powerful.<\/p>\n<p>In the following example, I also used <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2019a\/matlab\/ref\/timetable.html\">timetable<\/a> to specify other signals with different sample times.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2019Q2\/timetableFillDataset2.png\" alt=\"Timetable\" \/><\/p>\n<p>Simulating the model, we can confirm that no point is missing or duplicated:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2019Q2\/timetableFillDatasetResult2.png\" alt=\"Good results\" \/><\/p>\n<p><strong>Now it's your turn<\/strong><\/p>\n<p>What is your preferred way to load signals in a simulation? Let us know in the comments below.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2019Q2\/timetableFillDatasetResult2.png\" onError=\"this.style.display ='none';\" \/><\/div>\n<p>Today I decided to revisit a topic I covered a few years ago: loading discrete signals in a simulation.<br \/>\nLet's see what has been added in the last few years to help with the potential problems you... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2019\/08\/06\/loading-signals-in-timetable-format\/\">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],"tags":[578,576,498],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/9044"}],"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=9044"}],"version-history":[{"count":12,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/9044\/revisions"}],"predecessor-version":[{"id":9068,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/9044\/revisions\/9068"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=9044"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=9044"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=9044"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}