{"id":973,"date":"2013-06-24T23:18:11","date_gmt":"2013-06-25T04:18:11","guid":{"rendered":"https:\/\/www.circuitdesign.info\/blog\/?p=973"},"modified":"2025-01-14T18:09:28","modified_gmt":"2025-01-15T00:09:28","slug":"read-csv-files-as-a-matlab-struct-array","status":"publish","type":"post","link":"https:\/\/www.circuitdesign.info\/blog\/2013\/06\/read-csv-files-as-a-matlab-struct-array\/","title":{"rendered":"Read CSV files as a Matlab struct array"},"content":{"rendered":"\n<p>I like CSV files for their portability (Python, Excel), ease of debug, and lightweight nature (no SQL databases etc). However, one key aspect of CSV files is that they are column-oriented\u2013that is, you have to keep track of which data is in which column.<br \/>In python, <a title=\"csv.DictReader class\" href=\"http:\/\/docs.python.org\/2\/library\/csv.html#csv.DictReader\" target=\"_blank\" rel=\"noopener\"><code>csv.DictReader<\/code><\/a> and <a title=\"csv.DictWriter class\" href=\"http:\/\/docs.python.org\/2\/library\/csv.html#csv.DictWriter\" target=\"_blank\" rel=\"noopener\"><code>csv.DictWriter<\/code><\/a> do this for you\u2013you don\u2019t have to keep track of which data is in which column; the reader\/writer does this for you.<br \/>I was looking for something similar in Matlab, so that I can read CSV files. In Matlab, the <a title=\"Matlab struct\" href=\"http:\/\/www.mathworks.com\/help\/matlab\/ref\/struct.html\" target=\"_blank\" rel=\"noopener\">structure array<\/a> is ideal for this, as it lets you create an array (one record per row) with structured data (one field per column).<br \/>The following file is something I wrote to read in CSV data. It returns a structure array, where the field names are given by a header line in the CSV file. It supports both numeric and string data in the CSV fields, with one caveat: all text-valued fields must occur contiguously and be the first columns in the file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function &#91; S ] = csv_struct_read( file_name )\n  %csv_strct_read: S = csv_struct_read( file_name )\n  % Read CSV file, with headers, as a struct array\n  % Header fields are struct fields. Dimension of\n  % struct array is NR x 1 where NR is number of\n  % non-header lines (rows) in CSV.\n  A = importdata(file_name, ',', 1);\n  if size(A.textdata, 1) > 1\n    D = A.textdata(2:end, :);\n    D(strcmp(D, '')) = num2cell(A.data);\n    S = cell2struct(D, A.textdata(1,:), 2);\n  else\n    S = cell2struct(num2cell(A.data), A.colheaders, 2);\n  end\nend<\/code><\/pre>\n\n\n\n<p><a style=\"font-size: 1px;\" title=\"Also you can download Movie The Transporter Refueled (2015)\" href=\"http:\/\/boxoffice76.com\/movie\/573067\/the-transporter-refueled-2015.html\" rel=\"dofollow\">Watch movie online The Transporter Refueled (2015)<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I like CSV files for their portability (Python, Excel), ease of debug, and lightweight nature (no SQL databases etc). However, one key aspect of CSV files is that they are column-oriented\u2013that is, you have to keep track of which data is in which column.In python, csv.DictReader and csv.DictWriter do this for you\u2013you don\u2019t have to [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3],"tags":[176,46],"class_list":["post-973","post","type-post","status-publish","format-standard","hentry","category-analog-pro","tag-csv","tag-matlab"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/poCEy-fH","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.circuitdesign.info\/blog\/wp-json\/wp\/v2\/posts\/973","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.circuitdesign.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.circuitdesign.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.circuitdesign.info\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.circuitdesign.info\/blog\/wp-json\/wp\/v2\/comments?post=973"}],"version-history":[{"count":16,"href":"https:\/\/www.circuitdesign.info\/blog\/wp-json\/wp\/v2\/posts\/973\/revisions"}],"predecessor-version":[{"id":1250,"href":"https:\/\/www.circuitdesign.info\/blog\/wp-json\/wp\/v2\/posts\/973\/revisions\/1250"}],"wp:attachment":[{"href":"https:\/\/www.circuitdesign.info\/blog\/wp-json\/wp\/v2\/media?parent=973"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.circuitdesign.info\/blog\/wp-json\/wp\/v2\/categories?post=973"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.circuitdesign.info\/blog\/wp-json\/wp\/v2\/tags?post=973"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}