{"id":8072,"date":"2016-11-25T09:00:30","date_gmt":"2016-11-25T14:00:30","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=8072"},"modified":"2016-11-28T08:18:30","modified_gmt":"2016-11-28T13:18:30","slug":"creating-and-populating-a-simulink-dataset-from-simulink-model","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2016\/11\/25\/creating-and-populating-a-simulink-dataset-from-simulink-model\/","title":{"rendered":"Creating and Populating a Simulink Dataset from Simulink model"},"content":{"rendered":"<div class=\"content\"><!--introduction--><\/p>\n<p><em>Richard is Consulting Engineer at MathWorks focused on the Embedded Coder product for code generation, primarily in the Aerospace industry.<\/em><\/p>\n<p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/1016860-richard-ruff\">Richard&#8217;s<\/a> pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/58-routh-m-creating-and-populating-a-simulink-dataset-from-simulink-model\">Creating and Populating a Simulink Dataset from Simulink model<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/123124-stacey-gage\">Stacey Gage<\/a>.<\/p>\n<p><b>Pick<\/b><\/p>\n<p>Have you ever wanted to feed custom data to a Simulink model?  One approach is to create a test harness model that uses a <a title=\"https:\/\/www.mathworks.com\/help\/releases\/R2016a\/simulink\/ug\/overview-of-model-referencing.html (link no longer works)\">Model Reference block<\/a> to feed data to the model being evaluated.  This requires you to define the test vectors in the test harness model. This requires you to maintain another model and really doesn&#8217;t solve the issue of possibly needing to run multiple sets of inputs.  While this might be okay for one or two test cases, it is not feasible to create a harness model for each set of inputs if you have many test cases.  To address this, you can specify the model to load time and data values from the MATLAB Workspace. The limitation here is it requires all of the inputs to be of data type &#8216;double&#8217;.  That might be okay in a lot of cases, but what about the case where some of the inputs are an integer, or a boolean as seen in the example model below?<\/p>\n<p><!--\/introduction--><\/p>\n<p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Richard\/main_SimulinkDataset\/mBuiltInDataTypesModel.png\" alt=\"\"> <\/p>\n<p>How can you get around this limitation?<\/p>\n<p>If you work in Simulink and <a title=\"https:\/\/www.mathworks.com\/help\/releases\/R2016a\/simulink\/ug\/exporting-signal-data-using-signal-logging.html (link no longer works)\">log data<\/a> for post-simulation analysis, you are probably aware of datasets.  Datasets provide a structured container to store multiple time series with differing data types.  In turn, a dataset can be used to drive the <a title=\"https:\/\/www.mathworks.com\/help\/releases\/R2016a\/simulink\/ug\/specify-data-for-root-level-input-ports.html (link no longer works)\">inputs to a Simulink model<\/a>.  The issue then becomes how to create a dataset that contains the custom inputs versus time for the simulation. There are a number of <a title=\"https:\/\/www.mathworks.com\/help\/releases\/R2016a\/simulink\/slref\/simulink.simulationdata.dataset-class.html (link no longer works)\">classes and methods<\/a> available that you could use to perform this task &#8211; but that requires you to write the code.  This MATLAB File Exchange solves the problem.<\/p>\n<p>A number of utility functions are provided that allow the user to create a dataset based on the Inports of a model and then insert custom time based data.  The functions handle<\/p>\n<div>\n<ul>\n<li><a title=\"https:\/\/www.mathworks.com\/help\/releases\/R2016a\/simulink\/ug\/data-types-supported-by-simulink.html (link no longer works)\">built-in data types<\/a> such as &#8216;double&#8217;, &#8216;single&#8217;, &#8216;int32&#8217;, etc.<\/li>\n<li><a title=\"https:\/\/www.mathworks.com\/help\/releases\/R2016a\/simulink\/ug\/about-simulink-enumerations.html (link no longer works)\">enumerations<\/a><\/li>\n<li>scalar and array data<\/li>\n<\/ul>\n<\/div>\n<p>If the data provided does not match the expected data type for the Inport, it will be automatically cast to the correct type.<\/p>\n<p>Along with the utility functions, there are a number of test cases (i.e. examples) the user can use\/refer to when applying this to their models.<\/p>\n<p>There are some limitations the user should be aware of.  At the present time, the functions do not support <a title=\"https:\/\/www.mathworks.com\/help\/releases\/R2016a\/simulink\/ug\/buses.html (link no longer works)\">buses<\/a>, array of buses, or function calls.  These are checked for by the utility functions and ignored when creating the dataset.  Hopefully, Stacey will continue to update this post in the future to remove these limitations.<\/p>\n<p><b>Comments<\/b><\/p>\n<p>Give it a try and let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=8072#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/58-routh-m#comments\">comment<\/a> for Stacey.<\/p>\n<p><script language=\"JavaScript\"> <!-- \n    function grabCode_222507821981418c9489f2d7a1337ee9() {\n        \/\/ Remember the title so we can use it in the new page\n        title = document.title;\n\n        \/\/ Break up these strings so that their presence\n        \/\/ in the Javascript doesn't mess up the search for\n        \/\/ the MATLAB code.\n        t1='222507821981418c9489f2d7a1337ee9 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 222507821981418c9489f2d7a1337ee9';\n    \n        b=document.getElementsByTagName('body')[0];\n        i1=b.innerHTML.indexOf(t1)+t1.length;\n        i2=b.innerHTML.indexOf(t2);\n \n        code_string = b.innerHTML.substring(i1, i2);\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\n\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \n        \/\/ in the XML parser.\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\n        \/\/ doesn't go ahead and substitute the less-than character. \n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\n\n        copyright = 'Copyright 2016 The MathWorks, Inc.';\n\n        w = window.open();\n        d = w.document;\n        d.write('\n\n<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\n\n\\n');\n\n        d.title = title + ' (MATLAB code)';\n        d.close();\n    }   \n     --> <\/script><\/p>\n<p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><a href=\"javascript:grabCode_222507821981418c9489f2d7a1337ee9()\"><span style=\"font-size: x-small;        font-style: italic;\">Get<br \/>\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><\/p>\n<p class=\"footer\">\n      Published with MATLAB&reg; R2016b<\/p>\n<\/div>\n<p><!--\n222507821981418c9489f2d7a1337ee9 ##### SOURCE BEGIN #####\n%% Creating and Populating a Simulink Dataset from Simulink model\n%\n% _Richard is Consulting Engineer at MathWorks focused on the Embedded\n% Coder product for code generation, primarily in the Aerospace industry._\n% \n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/1016860-richard-ruff\n% Richard's> pick this week is\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/58-routh-m-creating-and-populating-a-simulink-dataset-from-simulink-model\n% Creating and Populating a Simulink Dataset from Simulink model> by\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/123124-stacey-gage\n% Stacey Gage>.\n% \n% *Pick*\n%\n% Have you ever wanted to feed custom data to a Simulink model?  One\n% approach is to create a test harness model that uses a\n% <https:\/\/www.mathworks.com\/help\/releases\/R2016a\/simulink\/ug\/overview-of-model-referencing.html\n% Model Reference block> to feed data to the model being evaluated.  This\n% requires you to define the test vectors in the test harness model. This\n% requires you to maintain another model and really doesn't solve the issue\n% of possibly needing to run multiple sets of inputs.  While this might be\n% okay for one or two test cases, it is not feasible to create a harness\n% model for each set of inputs if you have many test cases.  To address\n% this, you can specify the model to load time and data values from the\n% MATLAB Workspace. The limitation here is it requires all of the inputs to\n% be of data type 'double'.  That might be okay in a lot of cases, but what\n% about the case where some of the inputs are an integer, or a boolean\n% as seen in the example model below?\n%\n\n%%\n% \n% <<mBuiltInDataTypesModel.png>>\n% \n\n%%\n% How can you get around this limitation?\n%\n% If you work in Simulink and\n% <https:\/\/www.mathworks.com\/help\/releases\/R2016a\/simulink\/ug\/exporting-signal-data-using-signal-logging.html\n% log data> for post-simulation analysis, you are probably aware of\n% datasets.  Datasets provide a structured container to store multiple time\n% series with differing data types.  In turn, a dataset can be used to\n% drive the\n% <https:\/\/www.mathworks.com\/help\/releases\/R2016a\/simulink\/ug\/specify-data-for-root-level-input-ports.html\n% inputs to a Simulink model>.  The issue then becomes how to create a\n% dataset that contains the custom inputs versus time for the simulation.\n% There are a number of\n% <https:\/\/www.mathworks.com\/help\/releases\/R2016a\/simulink\/slref\/simulink.simulationdata.dataset-class.html\n% classes and methods> available that you could use to perform this task -\n% but that requires you to write the code.  This MATLAB File Exchange\n% solves the problem.\n% \n% A number of utility functions are provided that allow the user to create\n% a dataset based on the Inports of a model and then insert custom time\n% based data.  The functions handle\n%%\n% \n% * <https:\/\/www.mathworks.com\/help\/releases\/R2016a\/simulink\/ug\/data-types-supported-by-simulink.html built-in data types> such as 'double', 'single', 'int32', etc.\n% * <https:\/\/www.mathworks.com\/help\/releases\/R2016a\/simulink\/ug\/about-simulink-enumerations.html enumerations>\n% * scalar and array data\n% \n\n%%\n% If the data provided does not match the expected data type for the\n% Inport, it will be automatically cast to the correct type.\n%\n% Along with the utility functions, there are a number of test cases (i.e.\n% examples) the user can use\/refer to when applying this to their models.  \n%\n% There are some limitations the user should be aware of.  At the present\n% time, the functions do not support\n% <https:\/\/www.mathworks.com\/help\/releases\/R2016a\/simulink\/ug\/buses.html\n% buses>, array of buses, or function calls.  These are checked for by the\n% utility functions and ignored when creating the dataset.  Hopefully,\n% Stacey will continue to update this post in the future to remove these\n% limitations.\n%\n% *Comments*\n% \n% Give it a try and let us know what you think\n% <https:\/\/blogs.mathworks.com\/pick\/?p=8072#respond here> or leave a\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/58-routh-m#comments\n% comment> for Stacey.\n%\n \n\n##### SOURCE END ##### 222507821981418c9489f2d7a1337ee9\n--><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Richard\/main_SimulinkDataset\/mBuiltInDataTypesModel.png\" onError=\"this.style.display ='none';\" \/><\/div>\n<p>\nRichard is Consulting Engineer at MathWorks focused on the Embedded Coder product for code generation, primarily in the Aerospace industry.<br \/>\nRichard&#8217;s pick this week is Creating and Populating&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2016\/11\/25\/creating-and-populating-a-simulink-dataset-from-simulink-model\/\">read more >><\/a><\/p>\n","protected":false},"author":131,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/8072"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/131"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=8072"}],"version-history":[{"count":4,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/8072\/revisions"}],"predecessor-version":[{"id":8089,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/8072\/revisions\/8089"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=8072"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=8072"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=8072"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}