{"id":3117,"date":"2014-01-08T13:46:38","date_gmt":"2014-01-08T18:46:38","guid":{"rendered":"https:\/\/blogs.mathworks.com\/seth\/?p=3117"},"modified":"2014-01-08T13:46:38","modified_gmt":"2014-01-08T18:46:38","slug":"simulink-and-matlab-objects","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2014\/01\/08\/simulink-and-matlab-objects\/","title":{"rendered":"Simulink and MATLAB objects"},"content":{"rendered":"<p>Recently, I spoke with a user who needed to interface a Simulink model with an external flight simulator implemented as java classes. He was able to exchange data with his simulator at the MATLAB prompt, but was not sure how to make that happen in Simulink.<\/p>\r\n\r\n<p>Here is what I proposed.<\/p>\r\n\r\n<p><strong>The problem<\/strong><\/p>\r\n\r\n<p>Let's illustrate the problem with a very simple example using the <a href=\"http:\/\/docs.oracle.com\/javase\/6\/docs\/api\/java\/util\/Date.html\"><tt>Date<\/tt><\/a> class available in the <tt>java.util<\/tt> package. If you never worked with Java Libraries in MATLAB, you can get an overview in <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/using-java-libraries-in-matlab.html\">this section of the documentation<\/a>\r\n\r\n<p>In MATLAB, I can create a <tt>java.util.Date<\/tt> object:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q4\/objectCreation.png\" alt=\"Creating a java object in MATLAB\" \/><\/p>\r\n\r\n<p>Once the object is in the base workspace, I can call any of its method:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q4\/objectMethods.png\" alt=\"Calling methods of a java object in MATLAB\" \/><\/p>\r\n\r\n<p>This is simple... but in Simulink how to create the object once and access its methods every step?<\/p>\r\n\r\n<p><strong>The solution: A persistent object in the MATLAB Function Block<\/strong><\/p>\r\n\r\n<p>One easy way to implement something like that is using the <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/ug\/creating-an-example-model-that-uses-a-matlab-function-block.html\">MATLAB function block<\/a>.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q4\/MlFcnBlockinModel.png\" alt=\"MATLAB Function Block\" \/><\/p>\r\n\r\n<p>The MATLAB Function block works by converting MATLAB code to C code and executing it during the simulation.  The MATLAB function block can only generate code for a subset of the MATLAB language.  However, it can call outside the generated C code, back to MATLAB, to run the expressions that do not support code generation.  These functions are referred to as <em>extrinsic functions<\/em>, as they execute outside the generated C code.  This is useful if your block will only be used for simulation.<\/p>\r\n\r\n<p>When I use the MATLAB Function block for simulation only applications like this one (I do not need to generate code for an embedded system), I like to make all my code extrinsic. That way I do not need to worry about <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/ug\/design-considerations-when-writing-matlab-code-for-code-generation.html\">MATLAB Code Design Considerations for Code Generation<\/a>.<\/p>\r\n\r\n<p>To do this, I create a separate MATLAB function file and make it extrinsic. The code in the MATLAB Function block looks like:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q4\/MlFcnBlock.png\" alt=\"MATLAB Function Block\" \/><\/p>\r\n\r\n<p>In the extrinsic function <tt>myWrapper.m<\/tt>, I manage the object and implement my algorithm. To keep the object properties from step to step, I make it <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/persistent.html\">persistent<\/a>, and I initialize it only once using the <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/isempty.html\">isempty<\/a> function:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q4\/wrapperCode.png\" alt=\"MATLAB Function Block\" \/><\/p>\r\n\r\n<p>and that's it... I can now access all the methods of this object and its properties remain persistent from step to step.<\/p>\r\n\r\n<p><strong>Now it's your turn<\/strong><\/p>\r\n\r\n<p>Note that this is not the only way to implement such functionality. A <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/sfg\/writing-level-2-matlab-s-functions.html\">MATLAB Level 2 S-Function<\/a> would also give similar results.<\/p>\r\n\r\n<p>Give that a try and let us know what you think by leaving a <a href=\"https:\/\/blogs.mathworks.com\/seth\/?p=3117&#comment\">comment here<\/a>.<\/p>\r\n\r\n\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\/2013Q4\/wrapperCode.png\" onError=\"this.style.display ='none';\" \/><\/div><p>Recently, I spoke with a user who needed to interface a Simulink model with an external flight simulator implemented as java classes. He was able to exchange data with his simulator at the MATLAB... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2014\/01\/08\/simulink-and-matlab-objects\/\">read more >><\/a><\/p>","protected":false},"author":41,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[102,87,10,33],"tags":[356],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/3117"}],"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=3117"}],"version-history":[{"count":27,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/3117\/revisions"}],"predecessor-version":[{"id":3161,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/3117\/revisions\/3161"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=3117"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=3117"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=3117"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}