{"id":5306,"date":"2016-04-07T17:17:32","date_gmt":"2016-04-07T22:17:32","guid":{"rendered":"https:\/\/blogs.mathworks.com\/simulink\/?p=5306"},"modified":"2016-04-07T17:52:14","modified_gmt":"2016-04-07T22:52:14","slug":"tips-and-tricks-when-using-the-sim-command","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2016\/04\/07\/tips-and-tricks-when-using-the-sim-command\/","title":{"rendered":"Tips and tricks when using the sim command"},"content":{"rendered":"<p>Today, I want to share a few tips and tricks about when and how to use the <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/sim.html\"><tt>sim<\/tt><\/a> command.<\/p>\r\n\r\n<p><strong>The <tt>sim<\/tt> command vs. The Play Button<\/strong><\/p>\r\n\r\n<p>As you all know, there are two ways to simulate a model: using the play button, and using the <tt>sim<\/tt> command.<\/p>\r\n\r\n<p>With the play button, you have the following advantages:<\/p>\r\n\r\n<ul>\r\n\t<li>Pause, step forward and step backward using the <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/simulation-stepper.html\">Simulation Stepper<\/a><\/li>\r\n\t<li>Set <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/ug\/conditional-breakpoints.html\">conditional breakpoints<\/a> on signals<\/li>\r\n\t<li>Tune parameters while the simulation is running<\/li>\r\n\t<li><a href=\"https:\/\/www.mathworks.com\/help\/simulink\/ug\/displaying-block-outputs.html\">Display port values for debugging<\/a><\/li>\r\n\t<li>You can interact with the MATLAB command prompt while the simulation is running<\/li>\r\n<\/ul>\r\n\r\n<p>Those are very convenient when developing and debugging a model, but they come with a certain performance cost. In contrast, the <tt>sim<\/tt> command does not allow any of the above, allowing it to provide better performance.<\/p>\r\n\r\n<p>Let's look at a few tips that might facilitate your workflow when using the <tt>sim<\/tt> command.<\/p>\r\n\r\n<p><strong>Monitoring the Progress of a simulation<\/strong><\/p>\r\n\r\n<p>When using <tt>sim<\/tt> and the model is not open in the editor, you have no idea how the simulation is progressing. My colleague <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/answers\/contributors\/2448372-aleksey-trubitsyn\/\">Alex Trubitsyn<\/a> just shared with me a trick to monitor the progress of the simulation started using <tt>sim<\/tt>: use a <a title=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/timer-class.html (link no longer works)\">MATLAB timer<\/a>. In its simplest version, the code looks like:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/simulink\/files\/SIM_timer.png\" alt=\"Using a timer to monitor the progress of a simulation\" \/><\/p>\r\n\r\n<p><strong>Use the Simulation Metadata<\/strong><\/p>\r\n\r\n<p>How many of you are measuring the time it takes for your simulation to run by using <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/tic.html\"><tt>tic<\/tt><\/a> and <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/toc.html\"><tt>toc<\/tt><\/a> around <tt>sim<\/tt>? Or by placing tic-toc statements in the model InitFcn and stopFcn callbacks?<\/p>\r\n\r\n<p>With the <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/simulink.simulationmetadata-class.html\">simulation metadata<\/a>, there is no need to do that anymore.<\/p>\r\n\r\n<p>The Simulink.SimulationOutput object returned by <tt>sim<\/tt> contains a metadata field, which includes a <tt>TimingInfo<\/tt> sub-field. In this field, you can find timing for various phases of the simulation.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/simulink\/files\/SIM_timing.png\" alt=\"Timing info in simulation output\" \/><\/p>\r\n\r\n<p><strong>Tell <tt>sim<\/tt> to Capture Errors<\/strong><\/p>\r\n\r\n<p>Before R2016a, when a simulation through <tt>sim<\/tt> would error out, the logged data was lost and the script which invoked <tt>sim<\/tt> would stop. For those running long Monte-Carlo simulations overnight, you very likely ended up placing the <tt>sim<\/tt> command inside a try-catch statement.<\/p>\r\n\r\n<p>In R2016a, I recommend passing the <tt>CaptureErrors<\/tt> parameter to <tt>sim<\/tt>. If the simulation errors out, <tt>sim<\/tt> will not, but instead will include the error message in the simulation metadata.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/simulink\/files\/SIM_error.png\" alt=\"Capturing Errors\" \/><\/p>\r\n\r\n<p>One of the main advantages of this technique is that the simulation output object will contain the logged data up to the error, allowing you to do some debugging without re-running the simulation.<\/p>\r\n\r\n<p><strong>Enable Timeout<\/strong><\/p>\r\n\r\n<p>If you are running multiple simulations in a loop and are using a variable-step solver, you probably want to use the timeout parameter of <tt>sim<\/tt>. That way, if for some reason one simulation hangs and begins to take unexpectedly small time steps, it will not block the other simulations for too long.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/simulink\/files\/SIM_timeout.png\" alt=\"timeout\" \/><\/p>\r\n\r\n<p><strong>Now it's your turn<\/strong><\/p>\r\n\r\n<p>If you have other tricks facilitating the usage of the <tt>sim<\/tt> command, let us know by leaving a <a href=\"https:\/\/blogs.mathworks.com\/simulink\/?p=5306&#comment\">comment here<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img src=\"https:\/\/blogs.mathworks.com\/simulink\/files\/SIM_error.png\" class=\"img-responsive attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"\" decoding=\"async\" loading=\"lazy\" \/><\/div><p>Today, I want to share a few tips and tricks about when and how to use the sim command.\r\n\r\nThe sim command vs. The Play Button\r\n\r\nAs you all know, there are two ways to simulate a model: using the... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2016\/04\/07\/tips-and-tricks-when-using-the-sim-command\/\">read more >><\/a><\/p>","protected":false},"author":41,"featured_media":5312,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[143,33],"tags":[468,469],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/5306"}],"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=5306"}],"version-history":[{"count":26,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/5306\/revisions"}],"predecessor-version":[{"id":5418,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/5306\/revisions\/5418"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media\/5312"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=5306"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=5306"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=5306"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}