{"id":6609,"date":"2017-06-19T12:25:52","date_gmt":"2017-06-19T17:25:52","guid":{"rendered":"https:\/\/blogs.mathworks.com\/simulink\/?p=6609"},"modified":"2017-06-20T10:24:31","modified_gmt":"2017-06-20T15:24:31","slug":"logging-simulation-data-in-timetable-format","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2017\/06\/19\/logging-simulation-data-in-timetable-format\/","title":{"rendered":"Logging Simulation Data in Timetable format"},"content":{"rendered":"<p>In case you did not notice, MATLAB R2016b introduced a new type of table: <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/timetables.html\"><tt>timetables<\/tt><\/a>. Because Simulink logs data over time, we decided in R2017a to give the possibility to use this format for signal logging.<\/p>\n<p><strong>Configuring the Model<\/strong><\/p>\n<p>First, you need to enable logging for some signals. For this example, I chose a house heating example, where the simulation duration is a week. I log the inside and outside temperatures, along with the energy cost. Such a long application is particularly appropriate for timetables. You will see why later.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2017Q2\/enableLogging.png\" alt=\"Enable Logging\" \/><\/p>\n<p>Next, in the model configuration, ensure Signal Logging is enabled.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2017Q2\/loggingConfig.png\" alt=\"Enable Logging in dataset format\" \/><\/p>\n<p>Finally, in the model Configuration Parameters, switch from the Commonly Used Parameters tab to the <strong>All Parameters<\/strong> tab. Select the Data Import\/Export category, and scroll to the bottom. The <tt>DatasetSignalFormat<\/tt> parameter allows you to choose between <a title=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/timeseries-class.html (link no longer works)\"><tt>timeseries<\/tt><\/a> and <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/timetables.html\"><tt>timetables<\/tt><\/a>. (<em>Note: If you prefer, you can also type \"DatasetSignalFormat\" in the search box<\/em>)<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2017Q2\/datasetConfig.png\" alt=\"Enable Logging in dataset format and timetable\" \/><\/p>\n<p><strong>Exploring the Simulation Output<\/strong><\/p>\n<p>After simulating the model, a <a title=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/simulink.simulationdata.dataset-class.html (link no longer works)\">dataset<\/a> object is created in the MATLAB workspace.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2017Q2\/dataset_out.png\" alt=\"Logged dataset\" \/><\/p>\n<p>For example, we can access the first ten points of the timetable for the \"cost\" signal using:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2017Q2\/loggedTimetable.png\" alt=\"Logged dataset\" \/><\/p>\n<p><strong>Advantages of Timetables<\/strong><\/p>\n<p>As mentioned previously, the example used here simulates for a long period of time (7 days). Let's look at a few features of the timetables that are particularly useful for such simulation.<\/p>\n<p>The first obvious advantage is that you can select the units in which the time is displayed. <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2017Q2\/timeFormat.png\" alt=\"Time Format\" \/><\/p>\n<p>You have to admit that 6 days and 23 hours is easier to understand than 682560 seconds.<\/p>\n<p>One thing I like is the indexing based on time. For example, if I want the data between the 36th and 37th hours of the simulation, I can simply define a <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/timerange.html\">timerange<\/a> to index in the timetable:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2017Q2\/timerange.png\" alt=\"Time Range\" \/><\/p>\n<p>In a similar way, if I want to extract a few points around a specific instant, I can create a <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/withtol.html\">withtol subscript<\/a>. In the following example, I extract the points within a +\/- 2 minutes range around the 48th hour of simulation.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2017Q2\/withTol.png\" alt=\"time tolerance intexing\" \/><\/p>\n<p>Using <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/retime.html\">retime<\/a>, it is possible to aggregate data quite easily. The following line returns the average value for each day of simulation.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2017Q2\/dailyMean.png\" alt=\"Aggretating Daily Average\" \/><\/p>\n<p>Those are just a few basic examples. I recommend looking at the timetable documentation for more advanced maneuvers.<\/p>\n<p>In my case, based on the example <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/examples\/preprocess-and-explore-bicycle-count-data-using-timetable.html\">Preprocess and Explore Time-stamped Data Using timetable<\/a>, I have been able to group the data in four periods of the day (AM, PM, evening and night) and get an average of the outside temperature for each of those periods during the entire week the simulation lasted.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2017Q2\/sortExample.png\" alt=\"Grouping and mean\" \/><\/p>\n<p><strong>Now it's your turn<\/strong><\/p>\n<p>Have you already adopted or considering adopting timetables to log your simulation data? Let us know what you think of this new feature by leaving a comment below.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2017Q2\/sortExample.png\" onError=\"this.style.display ='none';\" \/><\/div>\n<p>In case you did not notice, MATLAB R2016b introduced a new type of table: timetables. Because Simulink logs data over time, we decided in R2017a to give the possibility to use this format for signal... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2017\/06\/19\/logging-simulation-data-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":[121,15,16],"tags":[215,498],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/6609"}],"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=6609"}],"version-history":[{"count":34,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/6609\/revisions"}],"predecessor-version":[{"id":6643,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/6609\/revisions\/6643"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=6609"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=6609"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=6609"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}