{"id":8840,"date":"2019-04-08T12:09:09","date_gmt":"2019-04-08T17:09:09","guid":{"rendered":"https:\/\/blogs.mathworks.com\/simulink\/?p=8840"},"modified":"2019-04-08T12:09:09","modified_gmt":"2019-04-08T17:09:09","slug":"executing-stateflow-charts-in-matlab","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2019\/04\/08\/executing-stateflow-charts-in-matlab\/","title":{"rendered":"Executing Stateflow Charts in MATLAB"},"content":{"rendered":"<p>In MATLAB R2019a, it is now possible to create standalone Stateflow charts that can be executed in MATLAB. Let's see how this work!<\/p>\n<p><strong>Getting Started<\/strong><\/p>\n<p>First, an important concept: A standalone Stateflow chart is a MATLAB class. The difference is that you edit it using the Stateflow graphical language instead of MATLAB code.<\/p>\n<p>To get started, use the <strong>Stateflow Chart<\/strong> entry under the <strong>New<\/strong> button in the MATLAB toolstrip:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2019Q2\/newStateflow.png\" alt=\"New Stateflow file\" \/><\/p>\n<p>The simplest example I can think of is the following, where I increase the value of a local variable \"x\" every time the chart executes.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2019Q2\/simplestChart.png\" alt=\"Simple chart\" \/><\/p>\n<p>I can execute this chart by first creating a chart object <tt>obj<\/tt> and initializing the value of <tt>x<\/tt> to zero. Then I can call the chart <tt>step<\/tt> method as many times I want and observe how the value of <tt>x<\/tt> increases.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2019Q2\/executingChart.png\" alt=\"Executing chart\" \/><\/p>\n<p>You can also add Event Inputs to charts, and they result in methods for the MATLAB class. In the following chart, I defined a <tt>RESET<\/tt> event and use it to reset the value of local variable <tt>x<\/tt>:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2019Q2\/chartWithFunction.png\" alt=\"Simple chart with event\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2019Q2\/callingChartWithFunction.png\" alt=\"calling chart with event\" \/><\/p>\n<p><strong>Designing MATLAB Apps logic in Stateflow<\/strong><\/p>\n<p>One of the things I like the most about standalone Stateflow Charts is that they team up very well with MATLAB Apps created using the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2019a\/matlab\/app-designer.html\">App Designer<\/a>.<\/p>\n<p>Let's extend our previous example a little bit. In App Designer, I created this simple App:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2019Q2\/simpleApp.png\" alt=\"MATLAB App\" \/><\/p>\n<p>My goal is that when I click the <tt>Enable<\/tt> button, the value displayed on <tt>TicksCount<\/tt> gauge will begin to increase by <tt>Increments<\/tt> and keep increasing every <tt>Delay<\/tt>. If I click the <tt>Reset<\/tt> button, <tt>TicksCount<\/tt> goes back to zero.<\/p>\n<p>How does this work?<\/p>\n<p>To connect the app with the Stateflow chart, they both need to know about the other one. On the app side, you can create a private property storing a handle to the chart. On the Stateflow side, you need to create a local variable that will contain a handle to the app. That way each of them will be able to call the other.<\/p>\n<p>In App Designer, the code will look like the following. The startupFcn initializes the Stateflow chart named <tt>appLogicChart<\/tt> and passes it the handle to the app. The chart object is then stored in the app property <tt>theChart<\/tt>.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2019Q2\/linkAppAndChart.png\" alt=\"Add registering a Stateflow chart\" \/><\/p>\n<p>With that done, the buttons in the app can call events in the chart.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2019Q2\/buttonCode.png\" alt=\"App Designer button callbacks\" \/><\/p>\n<p>At the same time, the chart can access the various properties of the app through its handle. Thanks to the Stateflow animation, it becomes very convenient to debug the logic in your app:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2019Q2\/MLSF.gif\" alt=\"Stateflow chart implementing logic of a MATLAB app\" \/><\/p>\n<p>One important thing to point out in the above chart is the usage of the <tt>after<\/tt> keyword. Under the hood, the chart sets up MATLAB <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2019a\/matlab\/ref\/timer-class.html\">timer<\/a> to implement the temporal logic. In my opinion, this makes it the simplest way to use timers in MATLAB.<\/p>\n<p><strong>Now it's your turn<\/strong><\/p>\n<p>If you are interested in this new feature, I recommend going through the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2019a\/stateflow\/execution-in-matlab.html\">Execution in MATLAB<\/a> section of the Stateflow documentation.<\/p>\n<p>We are curious to hear how you are going to leverage standalone Stateflow charts... Let us know in the comments below!<\/p>\n<p>Personally, I may never write a MATLAB class in the MATLAB Editor ever again!<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2019Q2\/MLSF.gif\" onError=\"this.style.display ='none';\" \/><\/div>\n<p>In MATLAB R2019a, it is now possible to create standalone Stateflow charts that can be executed in MATLAB. Let's see how this work!<br \/>\nGetting Started<br \/>\nFirst, an important concept: A standalone Stateflow... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2019\/04\/08\/executing-stateflow-charts-in-matlab\/\">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":[56,16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/8840"}],"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=8840"}],"version-history":[{"count":21,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/8840\/revisions"}],"predecessor-version":[{"id":8900,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/8840\/revisions\/8900"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=8840"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=8840"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=8840"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}