{"id":1428,"date":"2013-02-28T11:22:38","date_gmt":"2013-02-28T16:22:38","guid":{"rendered":"https:\/\/blogs.mathworks.com\/seth\/?p=1428"},"modified":"2013-02-28T14:58:03","modified_gmt":"2013-02-28T19:58:03","slug":"matlab-language-in-stateflow","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2013\/02\/28\/matlab-language-in-stateflow\/","title":{"rendered":"MATLAB Language in Stateflow"},"content":{"rendered":"<p>Have you noticed a new block in the Stateflow Library since R2012b?<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q1\/ML_chart_in_library.png\" alt=\"Chart (MATLAB)\" \/><\/p>\r\n\r\n<p>The <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2012b\/stateflow\/ref\/matlabchart.html\">MATLAB Chart<\/a> block uses <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2012b\/stateflow\/matlab-syntax-for-states-and-transitions.html\">MATLAB as the action language for states and transitions in Stateflow<\/a>.<\/p>\r\n\r\n<p>This means that you don't have to create a separate <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2012b\/stateflow\/ug\/building-a-model-with-a-matlab-function-in-a-chart.html\">MATLAB Function<\/a> every time you want to do matrix math in a Stateflow chart. Let's look at a few examples of what can be done in a MATLAB Chart.<\/p>\r\n\r\n<p><strong>Matrix Operations<\/strong><\/p>\r\n\r\n<p>Let's say I define a bus object containing a <tt>3x1<\/tt> position vector and a <tt>3x3<\/tt> rotation matrix:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q1\/bus_object_for_sf.png\" alt=\"Bus Object\" \/><\/p>\r\n\r\n<p>With MATLAB as the action language, I can multiply the bus elements directly in a transition action:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q1\/matrix_multiply_in_sf.png\" alt=\"matrix multiplication in stateflow\" \/><\/p>\r\n\r\n<p><strong>Access to all MATLAB Coder supported functions<\/strong><\/p>\r\n\r\n<p>With MATLAB as the action language, you can use any <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2012b\/simulink\/ug\/functions-supported-for-code-generation--alphabetical-list.html\">function supported by MATLAB Coder for code generation<\/a>. For example, I can use the functions like <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2012b\/matlab\/ref\/ones.html\">ones<\/a> and <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2012b\/matlab\/ref\/eig.html\">eig<\/a> in a transition:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q1\/matlabfunctions_in_sf.png\" alt=\"Using MATLAB Functions in transition actions\" \/><\/p>\r\n\r\n<p><strong>Manipulating Arrays of buses<\/strong><\/p>\r\n\r\n<p>Let's say I want my chart to generate an array of the bus defined previously, I could write something like:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q1\/array_of_buses_in_ML_chart.png\" alt=\"Manipulating arrays of buses in Stateflow\" \/><\/p>\r\n\r\n<p><strong>Conclusion<\/strong><\/p>\r\n\r\n<p>Based on my experience, using MATLAB as the action language in Stateflow charts makes it a lot easier to implement the algorithms I need. Since I first tried it, I have not needed to use C as the action language.<\/p>\r\n\r\n<p>Of course, the MATLAB Chart block is not the best choice for all applications. If your application is interfacing with legacy C functions or more naturally expressed in C language, than there is no need to switch. The <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2012b\/stateflow\/ref\/cchart.html\">C chart<\/a> block that we are all used to still continues to be developed and supported.<\/p>\r\n\r\n<p>If you need help deciding which block to use, I recommend looking at the <a href=\"https:\/\/www.mathworks.com\/help\/stateflow\/ug\/differences-between-matlab-and-stateflow-action-language.html\">Differences Between MATLAB and C as Action Language Syntax<\/a> in the Stateflow documentation.<\/p>\r\n\r\n<p><strong>Now it's your turn<\/strong><\/p>\r\n\r\n<p>Have you tried using MATLAB as the action language in your Stateflow charts? Let us know what you think by leaving a <a href=\"https:\/\/blogs.mathworks.com\/seth\/?p=1428&#comment\">comment here<\/a>.<\/p>\r\n\r\n\r\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q1\/array_of_buses_in_ML_chart.png\" onError=\"this.style.display ='none';\" \/><\/div><p>Have you noticed a new block in the Stateflow Library since R2012b?\r\n\r\n\r\n\r\nThe MATLAB Chart block uses MATLAB as the action language for states and transitions in Stateflow.\r\n\r\nThis means that you... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2013\/02\/28\/matlab-language-in-stateflow\/\">read more >><\/a><\/p>","protected":false},"author":41,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[56,16],"tags":[455],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/1428"}],"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=1428"}],"version-history":[{"count":40,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/1428\/revisions"}],"predecessor-version":[{"id":1950,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/1428\/revisions\/1950"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=1428"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=1428"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=1428"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}