{"id":865,"date":"2012-05-23T22:27:35","date_gmt":"2012-05-24T03:27:35","guid":{"rendered":"https:\/\/blogs.mathworks.com\/seth\/?p=865"},"modified":"2012-05-23T22:27:35","modified_gmt":"2012-05-24T03:27:35","slug":"how-to-make-your-own-blocks-with-code-introduction-to-s-functions","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2012\/05\/23\/how-to-make-your-own-blocks-with-code-introduction-to-s-functions\/","title":{"rendered":"How to make your own blocks with code! (Introduction to S-Functions)"},"content":{"rendered":"<p>I have to admit... before joining MathWorks, I was afraid of S-Functions.<\/p>\r\n\r\n<p>Now this makes me laugh, because I know that writing an S-Function is very powerful and not that complicated.<\/p>\r\n\r\n<p>In my opinion, understanding S-Functions is the best way to understand how Simulink works.<\/p>\r\n\r\n<p>When looking at the previous posts on this blog, I realized that I wrote a few posts about advanced usage of s-functions (<a href=\"https:\/\/blogs.mathworks.com\/seth\/2011\/06\/28\/variable-size-signals-and-unit-delay\/\">Variable Size Signals and Unit Delay<\/a> and <a href=\"https:\/\/blogs.mathworks.com\/seth\/2012\/01\/22\/advanced-s-function-techniques-scheduling-future-events\/\/\">Scheduling Future Events<\/a>), but I never covered the basic of how S-Functions work.<\/p>\r\n\r\n<p><strong>Getting Started with S-Functions<\/strong><\/p>\r\n\r\n<p>For most MATLAB users, I recommend starting by writing S-Functions in MATLAB (in opposite to C or C++). MATLAB S-Functions have some limitations compared to C or C++, but debugging is easier.<\/p>\r\n\r\n<p>Personally, I like to start with examples. In MATLAB, type:<\/p>\r\n\r\n<code>sfundemos<\/code>\r\n\r\n<p>This will launch a library containing links to many examples. In this collection, the simplest model to start with is <tt>msfcndemo_timestwo.mdl<\/tt>.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2012Q2\/sfundemos.png\" alt=\"S-Functions demos\"><\/p>\r\n\r\n<p>In this model, click on the annotation to open the source code of the S-Function.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2012Q2\/timestwo_model.png\" alt=\"Simple times two s-function demo\"><\/p>\r\n\r\n<p><strong>Basic structure of an S-Function<\/strong><\/p>\r\n\r\n<p>If you look at the code, you will find that the S-Function is in fact just a MATLAB function, taking as input a variable <tt>block<\/tt> and passing it to another function using the line <tt>setup(block)<\/tt>.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2012Q2\/timestwo_setup.png\" alt=\"Times two s-function code\"><\/p>\r\n\r\n<p><strong>The <tt>block<\/tt> variable<\/strong><\/p>\r\n\r\n<p>This <tt>block<\/tt> variable is an instance of the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2012a\/toolbox\/simulink\/slref\/simulink.msfcnruntimeblock.html \"><tt>Simulink.MSFcnRunTimeBlock<\/tt><\/a> class. We call it the S-Function run-time object.<\/p>\r\n\r\n<p>Using the run-time object, the S-Function can exchange information with the Simulink engine.<\/p>\r\n\r\n<p><strong>The <tt>setup(block)<\/tt> function<\/strong><\/p>\r\n\r\n<p>In the <tt>setup<\/tt> function, the run-time object allows you to specify and obtain information about various characteristics of the block, including ports, parameters, states, work vectors, etc.<\/p>\r\n\r\n<p>In our example <tt>msfcn_times_two.m<\/tt>, the properties of ports are set using lines like:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2012Q2\/timestwo_setup2.png\" alt=\"Setting properties of the block ports\"><\/p>\r\n\r\n<p>Also, the <tt>setup<\/tt> function is responsible for registering a set of <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2012a\/toolbox\/simulink\/sfg\/f7-67622.html\">callback methods<\/a> that the Simulink solver will call during different phases of the simulation. <\/p>\r\n\r\n<p>In the times two example, we register only one method, the <tt>output<\/tt> function:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2012Q2\/timestwo_setup3.png\" alt=\"Registering s-function callback methods\"><\/p>\r\n\r\n<p>If you are interested to see the list of available methods and when they are called, I recommend going through the documentation section <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2012a\/toolbox\/simulink\/sfg\/f8-37326.html\">How the Simulink Engine Interacts with C S-Functions<\/a><\/p>\r\n\r\n\r\n<p><strong>Registered Methods<\/strong><\/p>\r\n\r\n<p>Every time the Simulink engine calls a method of an S-Function, it passes to it the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2012a\/toolbox\/simulink\/slref\/simulink.msfcnruntimeblock.html \">run-time object<\/a>.<\/p>\r\n\r\n<p>In <tt>msfcn_times_two.m<\/tt>, we access the data of the input port and use it to compute the value that will be written to the output port:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2012Q2\/timestwo_output.png\" alt=\"Times two s-function output method\"><\/p>\r\n\r\n<p><strong>Conclusion<\/strong><\/p>\r\n\r\n<p>This is it for this simple s-function example.<\/p>\r\n\r\n<p>Of course, if you need to multiply a signal by two in Simulink, I recommend using a <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2012a\/toolbox\/simulink\/slref\/gain.html\">Gain<\/a> block, and not an S-Function. But hopefully this simple example gives you an idea of how MATLAB S-functions work.<\/p> \r\n\r\n<p><strong>Now it's your turn<\/strong><\/p>\r\n\r\n<p>Do you use S-Functions? Are there S-Functions related topics you would like to be covered in a future post? Leave a <a href=\"https:\/\/blogs.mathworks.com\/seth\/?p=865&#comment\">comment here<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>I have to admit... before joining MathWorks, I was afraid of S-Functions.\r\n\r\nNow this makes me laugh, because I know that writing an S-Function is very powerful and not that complicated.\r\n\r\nIn my... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2012\/05\/23\/how-to-make-your-own-blocks-with-code-introduction-to-s-functions\/\">read more >><\/a><\/p>","protected":false},"author":41,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[67,87],"tags":[459],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/865"}],"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=865"}],"version-history":[{"count":30,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/865\/revisions"}],"predecessor-version":[{"id":938,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/865\/revisions\/938"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=865"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=865"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=865"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}