{"id":2622,"date":"2011-01-21T12:43:23","date_gmt":"2011-01-21T12:43:23","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/2011\/01\/21\/reading-structured-data-files\/"},"modified":"2016-07-06T11:45:52","modified_gmt":"2016-07-06T15:45:52","slug":"reading-structured-data-files","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2011\/01\/21\/reading-structured-data-files\/","title":{"rendered":"Reading Structured Data Files"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\">Jiro<\/a>'s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/28564-nrgsymphonieread\"><tt>NRGSymphonieRead<\/tt><\/a> by a fellow application engineer, <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/39668\">Todd Schultz<\/a>, who sits down the hall from me.\r\n   <\/p>\r\n   <p>Have you ever needed to bring data into MATLAB from a structured data file, but found that the structure is a bit too complex\r\n      for the basic <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/f16-5702.html\">file I\/O functions<\/a> to work out of the box? In those cases, you typically have to write a reader function for your particular type of file. Todd\r\n      wrote this function to read text files produced from an NRG Symphonie data logger. But he used techniques that can be applied to work with other types of files. As long as there is some structure to the\r\n      file, it is possible to write a reader for it, and Todd's file demonstrates how to do so.\r\n   <\/p>\r\n   <p>In his entry, he included a sample file to test his code with. The file looks like this:<\/p><pre>   SDR\t6.0.14<\/pre><pre>   -----Logger Information-----\r\n   Model #\t3090\r\n   Serial #\t09351\r\n   Hardware Rev.\t011-010-000<\/pre><pre>   -----Site Information-----\r\n   Site #\t1029\r\n   Site Desc\tNew Site\r\n   Project Code\tNew\r\n   Project Desc\tNew Project\r\n   Site Location\r\n   Site Elevation\t41\r\n   Latitude\tN 042&deg; 14.348'\r\n   Longitude\tW 070&deg; 51.088'\r\n   Time offset (hrs)\t-5<\/pre><pre>   -----Sensor Information-----<\/pre><pre>   Channel #\t1\r\n   Type\t1\r\n   Description\tNRG #40 Anem. m\/s\r\n   Details\r\n   Serial Number\tSN:\r\n   Height\t49   m\r\n   Scale Factor\t0.765\r\n   Offset\t0.35\r\n   Units\tm\/s<\/pre><pre>   Channel #\t2\r\n   Type\t1\r\n   Description\tNRG #40 Anem. m\/s\r\n   Details\r\n   Serial Number\tSN:\r\n   Height\t49   m\r\n   Scale Factor\t0.765\r\n   Offset\t0.35\r\n   Units\tm\/s<\/pre><pre>      .\r\n      .\r\n      .<\/pre><pre>   Date &amp; Time Stamp\tCH1Avg\tCH1SD\tCH1Max\tCH1Min\t...\r\n   05\/25\/2007 11:00:00\t0.3500\t0.0000\t0.3500\t0.3500\t...\r\n   05\/25\/2007 11:10:00\t0.3500\t0.0000\t0.3500\t0.3500\t...\r\n   05\/25\/2007 11:20:00\t1.7473\t1.1298\t4.1707\t0.3500\t...<\/pre><pre>      .\r\n      .\r\n      .<\/pre><p>As you can see, it contains some header information, grouped by descriptive titles (\"Logger Information\", \"Site Information\",\r\n      \"Sensor Information\"), and then a block of data in a tabular form. Todd's function scans the file for the header information,\r\n      puts it into a structure variable, and extracts the tabular data into a <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/toolbox\/stats\/bqziht7-1.html#bqzihxq\">dataset array<\/a>. His code is very well commented and broken up into sections that handle different parts of the file. Anyone wanting to learn\r\n      how to write a similar reader for their particular data files should take a look at Todd's file. I found it helpful when I\r\n      ran this in <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/matlab_env\/f2-4931.html\">debug mode<\/a>. I could see what each step was doing and how the various data was being extracted.\r\n   <\/p>\r\n   <p><b>Comments<\/b><\/p>\r\n   <p>Tell us <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=2622#respond\">here<\/a> about any file reader functions you've written. You can also leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/28564-nrgsymphonieread#comments\">comment<\/a> for Todd.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_9734ef91b92f43d98229d2ee595530dd() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='9734ef91b92f43d98229d2ee595530dd ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 9734ef91b92f43d98229d2ee595530dd';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Jiro Doke';\r\n        copyright = 'Copyright 2011 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_9734ef91b92f43d98229d2ee595530dd()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.11<br><\/p>\r\n<\/div>\r\n<!--\r\n9734ef91b92f43d98229d2ee595530dd ##### SOURCE BEGIN #####\r\n%%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\r\n% Jiro>'s pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/28564-nrgsymphonieread\r\n% |NRGSymphonieRead|> by a fellow application engineer,\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/39668 Todd\r\n% Schultz>, who sits down the hall from me.\r\n%\r\n% Have you ever needed to bring data into MATLAB from a structured data\r\n% file, but found that the structure is a bit too complex for the basic\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/f16-5702.html\r\n% file I\/O functions> to work out of the box? In those cases, you typically\r\n% have to write a reader function for your particular type of file. Todd\r\n% wrote this function to read text files produced from an\r\n% <http:\/\/www.nrgsystems.com\/AllProducts\/DataLoggers.aspx NRG Symphonie\r\n% data logger>. But he used techniques that can be applied to work with\r\n% other types of files. As long as there is some structure to the file, it\r\n% is possible to write a reader for it, and Todd's file demonstrates how to\r\n% do so.\r\n%\r\n% In his entry, he included a sample file to test his code with. The file\r\n% looks like this:\r\n%\r\n%     SDR\t6.0.14\r\n% \r\n%     REPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASH-Logger InformationREPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASH-\r\n%     Model #\t3090\r\n%     Serial #\t09351\r\n%     Hardware Rev.\t011-010-000\r\n% \r\n%     REPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASH-Site InformationREPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASH-\r\n%     Site #\t1029\r\n%     Site Desc\tNew Site\r\n%     Project Code\tNew\r\n%     Project Desc\tNew Project\r\n%     Site Location\t\r\n%     Site Elevation\t41\r\n%     Latitude\tN 042\u00c2\u00b0 14.348'\r\n%     Longitude\tW 070\u00c2\u00b0 51.088'\r\n%     Time offset (hrs)\t-5\r\n% \r\n%     REPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASH-Sensor InformationREPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASH-\r\n% \r\n%     Channel #\t1\r\n%     Type\t1\r\n%     Description\tNRG #40 Anem. m\/s\r\n%     Details\t\r\n%     Serial Number\tSN:\r\n%     Height\t49   m\r\n%     Scale Factor\t0.765\r\n%     Offset\t0.35\r\n%     Units\tm\/s\r\n% \r\n%     Channel #\t2\r\n%     Type\t1\r\n%     Description\tNRG #40 Anem. m\/s\r\n%     Details\t\r\n%     Serial Number\tSN:\r\n%     Height\t49   m\r\n%     Scale Factor\t0.765\r\n%     Offset\t0.35\r\n%     Units\tm\/s\r\n%\r\n%        .\r\n%        .\r\n%        .\r\n% \r\n%     Date & Time Stamp\tCH1Avg\tCH1SD\tCH1Max\tCH1Min\t...\r\n%     05\/25\/2007 11:00:00\t0.3500\t0.0000\t0.3500\t0.3500\t...\r\n%     05\/25\/2007 11:10:00\t0.3500\t0.0000\t0.3500\t0.3500\t...\r\n%     05\/25\/2007 11:20:00\t1.7473\t1.1298\t4.1707\t0.3500\t...\r\n% \r\n%        .\r\n%        .\r\n%        .\r\n%\r\n% As you can see, it contains some header information, grouped by\r\n% descriptive titles (\"Logger Information\", \"Site Information\", \"Sensor\r\n% Information\"), and then a block of data in a tabular form. Todd's\r\n% function scans the file for the header information, puts it into a\r\n% structure variable, and extracts the tabular data into a\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010b\/toolbox\/stats\/bqziht7-1.html#bqzihxq\r\n% dataset array>. His code is very well commented and broken up into\r\n% sections that handle different parts of the file. Anyone wanting to learn\r\n% how to write a similar reader for their particular data files should take\r\n% a look at Todd's file. I found it helpful when I ran this in\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/matlab_env\/f2-4931.html\r\n% debug mode>. I could see what each step was doing and how the various\r\n% data was being extracted.\r\n%\r\n% *Comments*\r\n%\r\n% Tell us <https:\/\/blogs.mathworks.com\/pick\/?p=2622#respond here> about any\r\n% file reader functions you've written. You can also leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/28564-nrgsymphonieread#comments\r\n% comment> for Todd.\r\n\r\n##### SOURCE END ##### 9734ef91b92f43d98229d2ee595530dd\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   Jiro's pick this week is NRGSymphonieRead by a fellow application engineer, Todd Schultz, who sits down the hall from me.\r\n   \r\n   Have you ever needed to bring data into MATLAB from a... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2011\/01\/21\/reading-structured-data-files\/\">read more >><\/a><\/p>","protected":false},"author":35,"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\/2622"}],"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\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=2622"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2622\/revisions"}],"predecessor-version":[{"id":7645,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2622\/revisions\/7645"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=2622"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=2622"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=2622"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}