{"id":6685,"date":"2017-07-26T14:30:52","date_gmt":"2017-07-26T19:30:52","guid":{"rendered":"https:\/\/blogs.mathworks.com\/simulink\/?p=6685"},"modified":"2017-07-26T14:31:07","modified_gmt":"2017-07-26T19:31:07","slug":"logging-stateflow-data","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2017\/07\/26\/logging-stateflow-data\/","title":{"rendered":"Logging Stateflow Data and States Activity"},"content":{"rendered":"<p>Today I want to describe a feature that is super useful, but not known enough: Logging states activity and data in Stateflow. <\/p>\n<p><strong>Logging Stateflow Data<\/strong><\/p>\n<p>If you want to quickly enable or disable logging for variables and states, select <strong>Log Chart Signals...<\/strong> from the Simulation menu.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2017Q2\/LogChartSignals.png\" alt=\"Enabling Stateflow Logging\" \/><\/p>\n<p>This will give you a flat list of all the items you can possibly log in your chart.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2017Q2\/SF_signalLogging.png\" alt=\"Enabling Stateflow Logging\" \/><\/p>\n<p>See <a title=\"https:\/\/www.mathworks.com\/help\/releases\/R2017a\/stateflow\/ug\/configure-states-and-local-data-for-logging.html (link no longer works)\">this documentation page<\/a> for more ways of configuring Stateflow logging.<\/p>\n<p><strong>Logging Stateflow Data From Command Line<\/strong><\/p>\n<p>I know many of you will ask if it is possible to control Stateflow logging programmatically. For that, you can use the <a href=\"https:\/\/www.mathworks.com\/help\/stateflow\/api\/overview-of-the-stateflow-api.html\">Stateflow API<\/a>.<\/p>\n<p>Let's say I want to log the local variable <tt>down_th<\/tt> for the example model <a href=\"https:\/\/www.mathworks.com\/help\/stateflow\/examples\/modeling-an-automatic-transmission-controller.html\">sf_car<\/a>.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2017Q2\/sf_car.png\" alt=\"Logging sf_car\" \/><\/p>\n<p>All you have to do is get a handle to the Stateflow root object, use the <tt>find<\/tt> method to search for data or states, and set the logging property to true.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2017Q2\/logging_API.png\" alt=\"Logging sf_car programmatically\" \/><\/p>\n<p><strong>Visualizing logged data<\/strong><\/p>\n<p>Once logging is enabled for data and states, enable <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/gui\/signal-logging.html\">Signal Logging<\/a> in the model configuration. At the end of the simulation, a dataset object appears in the MATLAB workspace.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2017Q2\/statefowDataset.png\" alt=\"Logged dataset including Stateflow data\" \/><\/p>\n<p>You can easily plot the data using the plot method of the MATLAB timeseries objects inside the dataset:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2017Q2\/plotStateflow.png\" alt=\"Plot Stateflow logged data\" \/><\/p>\n<p>If you prefer, you can also directly stream the Stateflow data to the Simulation Data Inspector. For that, enable the <a title=\"https:\/\/www.mathworks.com\/help\/simulink\/ug\/record-simulation-data.html (link no longer works)\">Record logged workspace data in Simulation Data Inspector<\/a> from the model configuration. At the end of the simulation, the logged data will show up in the Simulation Data Inspector automatically.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2017Q2\/StateflowSDI.png\" alt=\"Stateflow logged data in SDI\" \/><\/p>\n<p><strong>Now it's your turn<\/strong><\/p>\n<p>Are you taking advantage of the Stateflow logging? Let us know what you think of this feature 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\/2017Q2\/StateflowSDI.png\" onError=\"this.style.display ='none';\" \/><\/div>\n<p>Today I want to describe a feature that is super useful, but not known enough: Logging states activity and data in Stateflow.<br \/>\nLogging Stateflow Data<br \/>\nIf you want to quickly enable or disable logging... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2017\/07\/26\/logging-stateflow-data\/\">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":[33,56],"tags":[215,321,320,501,455],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/6685"}],"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=6685"}],"version-history":[{"count":12,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/6685\/revisions"}],"predecessor-version":[{"id":6701,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/6685\/revisions\/6701"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=6685"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=6685"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=6685"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}