{"id":100,"date":"2010-07-18T19:11:07","date_gmt":"2010-07-18T19:11:07","guid":{"rendered":"https:\/\/blogs.mathworks.com\/seth\/2010\/07\/18\/including-matlab-code-in-a-simulation\/"},"modified":"2010-07-18T19:11:07","modified_gmt":"2010-07-18T19:11:07","slug":"including-matlab-code-in-a-simulation","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2010\/07\/18\/including-matlab-code-in-a-simulation\/","title":{"rendered":"Including MATLAB code in a simulation"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/guy_rouleau_small.png\" alt=\"Blogger, Guy Rouleau\"> By <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/31651\">Guy Rouleau<\/a><\/p>\r\n\r\n<p>Some algorithms are easier to implement using MATLAB code than blocks. This is why multiple <a href = \"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/toolbox\/simulink\/slref\/f4-4889.html#f4-5797\">User Defined Function<\/a> blocks are available in Simulink. But when time comes to implement MATLAB code in a Simulink model, how do you choose one? <\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2010Q3\/userDefinedFunctions.png\"><\/p>\r\n\r\n<p>I often see users making their life complicated because they choose the wrong block to implement MATLAB code. So today I want to share the process I follow to choose one block over another.<\/p>\r\n\r\n<p><strong>1: Look at the doc <\/strong><\/p>\r\n\r\n<p>The most important recommendation I can give is to look at the Simulink documentation section titled <a href = \"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/toolbox\/simulink\/ug\/bq3qcgr.html\">Comparison of Custom Block Functionality<\/a>. This documentation page compares all the options and helps choosing the most appropriate based on your requirements.<\/p>\r\n\r\n<p><strong>2: Determine if you need to generate code for the model<\/strong><\/p>\r\n\r\n<p>If you need to generate code for your model, there are 2 options:\r\n\r\n<ul>\r\n<li><a href = \"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/toolbox\/simulink\/slref\/fcn.html\">Fcn<\/a><\/li>\r\n<li><a href = \"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/toolbox\/simulink\/slref\/embeddedmatlabfunction.html\">Embedded MATLAB Function<\/a><\/li>\r\n<\/ul>\r\n\r\n<p>If you don't need to generate code, you have 2 additional options:<\/p>\r\n\r\n<ul>\r\n<li><a href = \"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/toolbox\/simulink\/slref\/matlabfcn.html\">MATLAB Fcn<\/a>.<\/li>\r\n<li><a href = \"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/toolbox\/simulink\/slref\/level2mfilesfunction.html\">Level-2 MATLAB-File S-Function<\/a><\/li>\r\n<\/ul>\r\n\r\n<p>Choosing among those brings me to a third piece of advice.<\/p>\r\n\r\n<p><strong>3: Keep it simple<\/strong><\/p>\r\n\r\n<p>If my algorithm can be implemented in one line using the subset of functions supported by the <a href = \"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/toolbox\/simulink\/slref\/fcn.html\">Fcn<\/a> block, I use it. This block is designed for simple mathematics expressions. It runs fast and can generate code. I personally use it when I have trigonometric or logical expressions to implement.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2010Q3\/FcnBlock.png\" alt=\"Fcn block\" \/><\/p>\r\n\r\n<p>The second option on my list is the <a href = \"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/toolbox\/simulink\/slref\/matlabfcn.html\">MATLAB Fcn<\/a>. To be honest, I almost never use this block. As the documentation for this block says, consider using the Fcn block or a Level 2 M-file S-function instead.<\/p>\r\n\r\n<p>If my algorithm is more complex, but does not have memory or states (the outputs depend only on the inputs), I will consider the <a href = \"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/toolbox\/simulink\/slref\/embeddedmatlabfunction.html\">Embedded MATLAB Function<\/a> block. <\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2010Q3\/EMLBlock.png\" alt=\"Embedded MATLAB Function block\" \/><\/p>\r\n\r\n<p>This block generates code for a <a href = \"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/toolbox\/eml\/ug\/bq1h2z7-9.html\">library of more that 300 MATLAB functions<\/a>. It can be configured to accept as many inputs and outputs as needed. Signals connected to it can be of any dimensions and data type, including buses. There is a little overhead the first time you update the model because it generates a MEX-file, but once it is compiled it runs fast.<\/p>\r\n\r\n<p>Sometimes I need my MATLAB code to interact closely with the Simulink solver. In this case, I go for an <a href = \"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/toolbox\/simulink\/sfg\/bsd99w_-1.html\">S-function<\/a>, typically a <a href = \"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/toolbox\/simulink\/slref\/level2mfilesfunction.html\">Level-2 MATLAB-File S-Function<\/a>. The S-function API allows you to interact closely with the <a href = \"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/toolbox\/simulink\/sfg\/f8-37326.html\">Simulink solver<\/a> and implementing everything the Simulink shipping blocks do.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2010Q3\/MATLAB_File_S_Function.png\" alt=\"Level 2 MATLAB File S-Function\" \/><\/p>\r\n\r\n<p>I have to admit that for a long time I was scared by S-functions because I thought they were too complex. Now, I realize I don't have to start from scratch.  I usually adapt one of the existing demo S-functions to meet my needs.  When I need an S-function, I type <a href = \"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/toolbox\/simulink\/sfg\/f7-67622.html#brgsb9k-1\">sfundemos<\/a> at the MATLAB command prompt to open a library of examples.<\/p>\r\n\r\n<p><strong>Now it's your turn<\/strong><\/p>\r\n\r\n<p>What is your preferred way to implement MATLAB code in a simulation? Share how you choose your implementation and leave a comment <a href=https:\/\/blogs.mathworks.com\/seth\/?p=100&#comment>here<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p> By Guy Rouleau\r\n\r\nSome algorithms are easier to implement using MATLAB code than blocks. This is why multiple User Defined Function blocks are available in Simulink. But when time comes to implement... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2010\/07\/18\/including-matlab-code-in-a-simulation\/\">read more >><\/a><\/p>","protected":false},"author":40,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[102,33],"tags":[145],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/100"}],"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\/40"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/comments?post=100"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/100\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=100"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=100"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=100"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}