{"id":2478,"date":"2016-06-27T14:31:53","date_gmt":"2016-06-27T19:31:53","guid":{"rendered":"https:\/\/blogs.mathworks.com\/videos\/?p=2478"},"modified":"2016-06-27T14:31:53","modified_gmt":"2016-06-27T19:31:53","slug":"using-trycatch-in-a-while-loop-to-repeatedly-execute-matlab-code","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/videos\/2016\/06\/27\/using-trycatch-in-a-while-loop-to-repeatedly-execute-matlab-code\/","title":{"rendered":"Using try\/catch in a While Loop to Repeatedly Execute MATLAB Code"},"content":{"rendered":"<p>You may be familiar with using <tt>try<\/tt>\/<tt>catch<\/tt> statements to make your code  robust to errors. Matt covered them in a <a href=\"https:\/\/blogs.mathworks.com\/videos\/2015\/09\/23\/reading-web-pages-part-1-using-webread\/\"> series of posts<\/a>. A <tt>try<\/tt>\/<tt>catch<\/tt> statement will catch one occurrence of an error in the <tt>try<\/tt> section and then continue to the <tt>catch<\/tt>. But what if you want to keep trying the same piece of code until it works? This video shows how to do this by inserting the <\/tt>try<\/tt>\/<tt>catch<\/tt> statement in a while loop.<\/p>\n<p>You can skip the section from 6:45 to 9:20, labeled in the table of contents &#8220;Breakpoint Not Working&#8221; to &#8220;Continue Stepping Through tryfunc&#8221; where I struggle to understand why something is not working. I eventually explain what is going on @15:45, labeled &#8220;Explanation of Breakpoint not working&#8221; in the table of contents.<\/p>\n<p><div class=\"row\"><div class=\"col-xs-12 containing-block\"><div class=\"bc-outer-container add_margin_20\"><videoplayer><div class=\"video-js-container\"><video data-video-id=\"4933155639001\" data-video-category=\"blog\" data-autostart=\"false\" data-account=\"62009828001\" data-omniture-account=\"mathwgbl\" data-player=\"rJ9XCz2Sx\" data-embed=\"default\" id=\"mathworks-brightcove-player\" class=\"video-js\" controls><\/video><script src=\"\/\/players.brightcove.net\/62009828001\/rJ9XCz2Sx_default\/index.min.js\"><\/script><script>if (typeof(playerLoaded) === 'undefined') {var playerLoaded = false;}(function isVideojsDefined() {if (typeof(videojs) !== 'undefined') {videojs(\"mathworks-brightcove-player\").on('loadedmetadata', function() {playerLoaded = true;});} else {setTimeout(isVideojsDefined, 10);}})();<\/script><\/div><\/videoplayer><\/div><\/div><\/div><\/p>\n<p>Play the video in full screen mode for a better viewing experience.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"thumbnail thumbnail_asset asset_overlay video\"><a href=\"https:\/\/blogs.mathworks.com\/videos\/2016\/06\/27\/using-trycatch-in-a-while-loop-to-repeatedly-execute-matlab-code\/?dir=autoplay\"><img decoding=\"async\" src=\"https:\/\/cf-images.us-east-1.prod.boltdns.net\/v1\/static\/62009828001\/12d1bea8-1a31-4d91-b59f-c19cb8379873\/9bb98a93-ee8b-42bb-9978-5cd1fda63180\/1280x720\/match\/image.jpg\" onError=\"this.style.display ='none';\"\/><\/p>\n<div class=\"overlay_container\">\n      <span class=\"icon-video icon_color_null\"><time class=\"video_length\">17:50<\/time><\/span>\n      <\/div>\n<p>      <\/a><\/div>\n<p>You may be familiar with using try\/catch statements to make your code  robust to errors. Matt covered them in a  series of posts. A try\/catch statement will catch one occurrence of an error in the&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/videos\/2016\/06\/27\/using-trycatch-in-a-while-loop-to-repeatedly-execute-matlab-code\/\">read more >><\/a><\/p>\n","protected":false},"author":133,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/posts\/2478"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/users\/133"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/comments?post=2478"}],"version-history":[{"count":8,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/posts\/2478\/revisions"}],"predecessor-version":[{"id":2488,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/posts\/2478\/revisions\/2488"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/media?parent=2478"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/categories?post=2478"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/tags?post=2478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}