{"id":253,"date":"2017-09-13T11:39:10","date_gmt":"2017-09-13T14:39:10","guid":{"rendered":"http:\/\/www.professores.uff.br\/diomarcesarlobao\/?page_id=253"},"modified":"2017-09-13T11:39:10","modified_gmt":"2017-09-13T14:39:10","slug":"readcoldata-matlab-script","status":"publish","type":"page","link":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/readcoldata-matlab-script\/","title":{"rendered":"readColData Matlab script"},"content":{"rendered":"<pre>function  [labels,x,y] = readColData(fname,ncols,nhead,nlrows)\r\n%  readColData  reads data from a file containing data in columns\r\n%               that have text titles, and possibly other header text\r\n%\r\n%  Synopsis:\r\n%     [labels,x,y] = readColData(fname)\r\n%     [labels,x,y] = readColData(fname,ncols)\r\n%     [labels,x,y] = readColData(fname,ncols,nhead)\r\n%     [labels,x,y] = readColData(fname,ncols,nhead,nlrows)\r\n%   \r\n%  Input:\r\n%     fname  = name of the file containing the data (required)\r\n%     ncols  = number of columns in the data file.  Default = 2.  A value\r\n%              of ncols is required only if nlrows is also specified.\r\n%     nhead  = number of lines of header information at the very top of\r\n%              the file.  Header text is read and discarded.  Default = 0.\r\n%              A value of nhead is required only if nlrows is also specified.\r\n%     nlrows = number of rows of labels.  Default = 1\r\n%\r\n%  Output:\r\n%     labels  =  matrix of labels.  Each row of lables is a different\r\n%                label from the columns of data.  The number of columns\r\n%                in the labels matrix equals the length of the longest\r\n%                column heading in the data file.  More than one row of\r\n%                labels is allowed.  In this case the second row of column\r\n%                headings begins in row ncol+1 of labels.  The third row\r\n%                column headings begins in row 2*ncol+1 of labels, etc.\r\n%\r\n%          NOTE:  Individual column headings must not contain blanks\r\n%\r\n%     x = column vector of x values\r\n%     y = matrix of y values.  y has length(x) rows and ncols columns\r\n%\r\n%  Author:\r\n%     Gerald Recktenwald, gerry@me.pdx.edu\r\n%     Portland State University, Mechanical Engineering Department\r\n%     24 August 1995\r\n\r\n%  process optional arguments\r\nif nargin &lt; 4\r\n   nlrows = 1;      % default\r\n   if nargin &lt; 3\r\n      nhead = 0;     % default\r\n      if nargin &lt; 2\r\n         ncols = 2;   % default\r\n      end\r\n   end\r\nend\r\n\r\n%  open file for input, include error handling\r\nfin = fopen(fname,'r');\r\nif fin &lt; 0\r\n   error(['Could not open ',fname,' for input']);\r\nend\r\n\r\n%  Preliminary reading of titles to determine number of columns\r\n%  needed in the labels matrix.  This allows for an arbitrary number\r\n%  of column titles with unequal (string) lengths.  We cannot simply\r\n%  append to the labels matrix as new labels are read because the first\r\n%  label might not be the longest.  The number of columns in the labels\r\n%  matrix (= maxlen) needs to be set properly from the start.\r\n\r\n%  Read and discard header text on line at a time\r\nfor i=1:nhead,  buffer = fgetl(fin);  end\r\n\r\nmaxlen = 0;\r\nfor i=1:nlrows\r\n   buffer = fgetl(fin);          %  get next line as a string\r\n   for j=1:ncols\r\n      [next,buffer] = strtok(buffer);       %  parse next column label\r\n      maxlen = max(maxlen,length(next));   %  find the longest so far\r\n   end\r\n   \r\nend\r\n\r\n%  Set the number of columns in the labels matrix equal to the length\r\n%  of the longest column title.  A complete preallocation (including\r\n%  rows) of the label matrix is not possible since there is no string\r\n%  equivalent of the ones() or zeros() command.  The blank() command\r\n%  only creates a string row vector not a matrix.\r\nlabels = blanks(maxlen);\r\n\r\nfrewind(fin);    %  rewind in preparation for actual reading of labels and data\r\n\r\n%  Read and discard header text on line at a time\r\nfor i=1:nhead,  buffer = fgetl(fin);  end\r\n\r\n%  Read titles for keeps this time\r\nfor i=1:nlrows\r\n\r\n   buffer = fgetl(fin);          %  get next line as a string\r\n   for j=1:ncols\r\n      [next,buffer] = strtok(buffer);     %  parse next column label\r\n      n = j + (i-1)*ncols;                %  pointer into the label array for next label\r\n      labels(n,1:length(next)) = next;    %  append to the labels matrix\r\n   end\r\nend\r\n\r\n%  Read in the x-y data.  Use the vetorized fscanf function to load all\r\n%  numerical values into one vector.  Then reshape this vector into a\r\n%  matrix before copying it into the x and y matrices for return.\r\n\r\ndata = fscanf(fin,'%f');  %  Load the numerical values into one long vector\r\n\r\nnd = length(data);        %  total number of data points\r\nnr = nd\/ncols;            %  number of rows; check (next statement) to make sure\r\nif nr ~= round(nd\/ncols)\r\n   fprintf(1,'\\ndata: nrow = %f\\tncol = %d\\n',nr,ncols);\r\n   fprintf(1,'number of data points = %d does not equal nrow*ncol\\n',nd);\r\n   error('data is not rectangular')\r\nend\r\n\r\ndata = reshape(data,ncols,nr)';   %  notice the transpose operator\r\nx = data(:,1);\r\ny = data(:,2:ncols);\r\n\r\n%  end of readColData.m\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>function [labels,x,y] = readColData(fname,ncols,nhead,nlrows) % readColData reads data from a file containing data in columns % that have text titles, and possibly other header text % % Synopsis: % [labels,x,y] = readColData(fname) % [labels,x,y] = readColData(fname,ncols) % [labels,x,y] = readColData(fname,ncols,nhead) % [labels,x,y] = readColData(fname,ncols,nhead,nlrows) % % Input: % fname = name of the file containing [&hellip;]<\/p>\n","protected":false},"author":22,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[],"tags":[],"class_list":["post-253","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/pages\/253","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/users\/22"}],"replies":[{"embeddable":true,"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/comments?post=253"}],"version-history":[{"count":1,"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/pages\/253\/revisions"}],"predecessor-version":[{"id":254,"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/pages\/253\/revisions\/254"}],"wp:attachment":[{"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/media?parent=253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/categories?post=253"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/tags?post=253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}