{"id":395,"date":"2012-02-09T15:18:17","date_gmt":"2012-02-09T20:18:17","guid":{"rendered":"https:\/\/blogs.mathworks.com\/seth\/?p=395"},"modified":"2019-08-07T14:46:57","modified_gmt":"2019-08-07T19:46:57","slug":"using-discrete-data-as-an-input-to-your-simulink-model","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2012\/02\/09\/using-discrete-data-as-an-input-to-your-simulink-model\/","title":{"rendered":"Using Discrete Data as an input to your Simulink model"},"content":{"rendered":"<p><strong>August 2019 Update:<\/strong> <em>See blog post <a href=\"https:\/\/blogs.mathworks.com\/simulink\/2019\/08\/06\/loading-signals-in-timetable-format\/\">Loading Signals in Timetable Format<\/a> for an improved way to address the issue described in this post.<\/em><\/p>\r\n\r\n<p>-----------------<\/p>\r\n\r\n<p>Today I want to look at a problem that often frustrates Simulink users who have discrete inputs to their model.<\/p>\r\n\r\n<p><strong>The Problem<\/strong><\/p>\r\n\r\n<p>I create a simple model with just an <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011b\/toolbox\/simulink\/slref\/inport.html\">Inport<\/a> block connected directly to an <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011b\/toolbox\/simulink\/slref\/outport.html\">Outport<\/a> block, both configured to execute at a sample time of 0.001 second.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2012Q1\/simplestModel.png\" alt=\"Simplest Possible Model\"\/><\/p>\r\n\r\n<p>I configure the model to import data from the workspace:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2012Q1\/InputData.png\" alt=\"Configuring a model to Imprt data from workspace\"\/><\/p>\r\n\r\n<p>I simulate the model and compare the input with the output using this code:<\/p>\r\n\r\n<code>ts = 0.001;<br>\r\nt = (0:ts:10)';<br>\r\nu = (1:length(t))';<br>\r\nsimOut = sim('exampleModel');<br> \r\nstem(abs(u-simOut.get('y').signals.values))<br><\/code>\r\n\r\n<p>For some samples, the output does not match the input.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2012Q1\/compareInOut.png\" alt=\"Comparing input and Output\"\/><\/p>\r\n\r\n<p><strong>The explanation<\/strong><\/p>\r\n\r\n<p>To understand what is happening here, it is important to compare the time vector of the input data and the time vector used by Simulink.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2012Q1\/compareTime.png\" alt=\"Comparing time vectors\"\/><\/p>\r\n\r\n<p>As we can see in this image, the steps taken by the Simulink solver are different from the ones specified in the input time vector. As explained in <a href=\"https:\/\/www.mathworks.com\/support\/solutions\/en\/data\/1-1ITHX9\/index.html\">technical solution 1-1ITHX9<\/a>, Simulink computes time using the following equation:<\/p>\r\n\r\n<code>\r\ntime = stepSize * [0:N]'\r\n<\/code>\r\n\r\n<p>While these two methods look similar, they can give different results due to floating point round-off. As you can image, if the input data is slightly behind or before the step taken by the solver, a data point can be skipped or repeated.<\/p>\r\n\r\n<p><strong>The Solution<\/strong><\/p>\r\n\r\n<p>To avoid such problem, it is recommended to NOT provide the time vector when inputting discrete data into a simulation. For example, modifying the code to use a structure without time will allow the Inport to take the next sample from the vector at each step.  Here is some code to create a signal structure without time, update the model to use this structure, and verify that the input and output are equal.<\/p>\r\n\r\n<code>inputStruct.time = [];<br>\r\ninputStruct.signals.dimensions = 1;<br>\r\ninputStruct.signals.values = u;<br>\r\nset_param('exampleModel','ExternalInput','inputStruct')<br>\r\nsimOut = sim('exampleModel'); <br>\r\nnorm(u-simOut.get('y').signals.values)<br>\r\n<br>\r\nans =<br>\r\n<br>\r\n     0<\/code>\r\n\r\n<p><strong>Now it's your turn<\/strong><\/p>\r\n\r\n<p>Look at the doc section <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011b\/toolbox\/simulink\/ug\/bsuwprb-1.html\">Guidelines for Specifying Time and Signal Values for Imported Data<\/a> for more details more this topic.<\/p>\r\n\r\n<p>Let us know how you import data into your models by leaving a <a href=\"https:\/\/blogs.mathworks.com\/seth\/?p=395&amp;#comment\">comment here<\/a>.<\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>August 2019 Update: See blog post Loading Signals in Timetable Format for an improved way to address the issue described in this post.\r\n\r\n-----------------\r\n\r\nToday I want to look at a problem that... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2012\/02\/09\/using-discrete-data-as-an-input-to-your-simulink-model\/\">read more >><\/a><\/p>","protected":false},"author":41,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,33],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/395"}],"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=395"}],"version-history":[{"count":34,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/395\/revisions"}],"predecessor-version":[{"id":9106,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/395\/revisions\/9106"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=395"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=395"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=395"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}