{"id":3833,"date":"2018-08-10T12:55:55","date_gmt":"2018-08-10T17:55:55","guid":{"rendered":"https:\/\/blogs.mathworks.com\/videos\/?p=3833"},"modified":"2019-07-31T15:25:33","modified_gmt":"2019-07-31T20:25:33","slug":"reading-whole-lines-of-text-from-a-file","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/videos\/2018\/08\/10\/reading-whole-lines-of-text-from-a-file\/","title":{"rendered":"Reading Whole Lines of Text from a File"},"content":{"rendered":"Sometimes the text files I need to read have an irregular format, i.e. it changes row-to-row. MATLAB file reading functions that expect columns of data (e.g <tt>readtable<\/tt>), need a uniform format to work. So if this is not the case, you need to read the file line-by-line, then split the lines\/rows up into separate elements inside MATLAB. This gives you the ability to handle structure of any complexity.\r\n\r\nMy colleague Jeremy, who works on MATLAB file I\/O, recommended these parameters to use as the safest way to read in text files a line at a time:\r\n<pre>Format = '%[^\\r\\n]', Delimiter = '' and EndOfLine = '\\r\\n',\r\n<\/pre>\r\nIn this <a href=\"https:\/\/blogs.mathworks.com\/videos\/2015\/10\/29\/matlab-code-along-videos\/\">code-along<\/a> style video, I try to read a large text file using this method and making use of these functions:\r\n<ul>\r\n \t<li><tt>textscan<\/tt><\/li>\r\n \t<li><tt>contains<\/tt>, <tt>split<\/tt>, <tt>exractBefore<\/tt><\/li>\r\n \t<li><tt>datetime<\/tt><\/li>\r\n<\/ul>\r\n\r\n\r\n<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=\"5820580799001\" 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>\r\n\r\nPlay the video in full screen mode for a better viewing experience.","protected":false},"excerpt":{"rendered":"<div class=\"thumbnail thumbnail_asset asset_overlay video\"><a href=\"https:\/\/blogs.mathworks.com\/videos\/2018\/08\/10\/reading-whole-lines-of-text-from-a-file\/?dir=autoplay\"><img decoding=\"async\" src=\"https:\/\/cf-images.us-east-1.prod.boltdns.net\/v1\/static\/62009828001\/bec5673d-d752-4801-873a-94427db17d32\/005a0cf9-14a8-4a0d-831e-a61ab01dc68a\/1280x720\/match\/image.jpg\" onError=\"this.style.display ='none';\"\/>\n      <div class=\"overlay_container\">\n      <span class=\"icon-video icon_color_null\"><time class=\"video_length\">29:58<\/time><\/span>\n      <\/div>\n      <\/a><\/div><p>Sometimes the text files I need to read have an irregular format, i.e. it changes row-to-row. MATLAB file reading functions that expect columns of data (e.g readtable), need a uniform format to work.... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/videos\/2018\/08\/10\/reading-whole-lines-of-text-from-a-file\/\">read more >><\/a><\/p>","protected":false},"author":133,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[27,4],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/posts\/3833"}],"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=3833"}],"version-history":[{"count":19,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/posts\/3833\/revisions"}],"predecessor-version":[{"id":4340,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/posts\/3833\/revisions\/4340"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/media?parent=3833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/categories?post=3833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/tags?post=3833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}