<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/174153</link>
    <title>MATLAB Central Newsreader - determine col num from column headers</title>
    <description>Feed for thread: determine col num from column headers</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Mon, 11 Aug 2008 19:02:03 -0400</pubDate>
      <title>determine col num from column headers</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/174153#448596</link>
      <author>Keli </author>
      <description>Hello!&lt;br&gt;
I have 5 data sets containing 25 files each with 465 to &lt;br&gt;
517 columns, and I need to read 4 specific columns from &lt;br&gt;
each file. The files are loaded from *.csv into &lt;br&gt;
seperate 'colheader' string array and the data matrix.  &lt;br&gt;
While each data set tends to have the same number of &lt;br&gt;
columns, thus far I need to manually view 'colheader' data &lt;br&gt;
string from each file in the data set to be certain the &lt;br&gt;
target columns are indeed the same column number.  Once I &lt;br&gt;
am certain of the column number I load this column into a &lt;br&gt;
seperate vector to use in other Matlab operations. Any &lt;br&gt;
suggestions on how can I determine the column number? Can &lt;br&gt;
I use the column headers (the 'colheader' string) to &lt;br&gt;
locate the specific column numbers?  &lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Thanks much!</description>
    </item>
    <item>
      <pubDate>Tue, 12 Aug 2008 08:55:05 -0400</pubDate>
      <title>Re: determine col num from column headers</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/174153#448668</link>
      <author>Andres </author>
      <description>&quot;Keli &quot; &amp;lt;krishnakeli@gmail.com&amp;gt; wrote in message &lt;br&gt;
&amp;lt;g7q2bb$ffi$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Hello!&lt;br&gt;
&amp;gt; I have 5 data sets containing 25 files each with 465 to &lt;br&gt;
&amp;gt; 517 columns, and I need to read 4 specific columns from &lt;br&gt;
&amp;gt; each file. The files are loaded from *.csv into &lt;br&gt;
&amp;gt; seperate 'colheader' string array and the data matrix.  &lt;br&gt;
&amp;gt; While each data set tends to have the same number of &lt;br&gt;
&amp;gt; columns, thus far I need to manually view 'colheader' &lt;br&gt;
data &lt;br&gt;
&amp;gt; string from each file in the data set to be certain the &lt;br&gt;
&amp;gt; target columns are indeed the same column number.  Once I &lt;br&gt;
&amp;gt; am certain of the column number I load this column into a &lt;br&gt;
&amp;gt; seperate vector to use in other Matlab operations. Any &lt;br&gt;
&amp;gt; suggestions on how can I determine the column number? Can &lt;br&gt;
&amp;gt; I use the column headers (the 'colheader' string) to &lt;br&gt;
&amp;gt; locate the specific column numbers?  &lt;br&gt;
&lt;br&gt;
Hi,&lt;br&gt;
&lt;br&gt;
we need more information about your colheader string, but &lt;br&gt;
here's a hint to an automation routine that might work, as &lt;br&gt;
a best guess:&lt;br&gt;
&lt;br&gt;
You know the exact distinct column title strings appearing &lt;br&gt;
in your header line string&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;myColumnNames = {'Val1','Val2','Val3','Val4'};&lt;br&gt;
&lt;br&gt;
If your header string contains more than one line, you &lt;br&gt;
should extract the line that contains your column names. &lt;br&gt;
Maybe you can identify the lines by the positions of a line &lt;br&gt;
break character:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;lineBreakPos = strfind(colheader,char(10));&lt;br&gt;
&lt;br&gt;
Find out somehow which line has the names, perhaps you know &lt;br&gt;
it beforehand. Assuming the k-th line is the line of &lt;br&gt;
interest, do something like:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;columnNameLine = colheader(lineBreakPos(k-1)+1: &lt;br&gt;
lineBreakPos(k)-1 );&lt;br&gt;
&lt;br&gt;
(take obvious precautions if it is the first or last line &lt;br&gt;
in colheader)&lt;br&gt;
&lt;br&gt;
Most probably there is a distinct delimiter (Tab, &lt;br&gt;
space, ';', ',' ...) between the column name strings. Find &lt;br&gt;
its positions in the line ...&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;delim = ';';&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;delimPos = strfind(columnNameLine,delim);&lt;br&gt;
&lt;br&gt;
find out the positions of your column name strings ...&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;namePos(n) = strfind(columnNameLine,myColumnNames(n));&lt;br&gt;
&lt;br&gt;
and compare them to the delimiter positions to make out the &lt;br&gt;
corresponding column index of your data matrix.&lt;br&gt;
&lt;br&gt;
I hope this gets you started.&lt;br&gt;
Best regards&lt;br&gt;
Andres</description>
    </item>
    <item>
      <pubDate>Tue, 12 Aug 2008 19:58:01 -0400</pubDate>
      <title>Re: determine col num from column headers</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/174153#448779</link>
      <author>Keli </author>
      <description>&quot;Andres &quot; &amp;gt; wrote in message &lt;br&gt;
&amp;gt; &amp;lt;g7q2bb$ffi$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; Hello!&lt;br&gt;
&amp;gt; &amp;gt; I have 5 data sets containing 25 files each with 465 &lt;br&gt;
to &lt;br&gt;
&amp;gt; &amp;gt; 517 columns, and I need to read 4 specific columns &lt;br&gt;
from &lt;br&gt;
&amp;gt; &amp;gt; each file. The files are loaded from *.csv into &lt;br&gt;
&amp;gt; &amp;gt; seperate 'colheader' string array and the data &lt;br&gt;
matrix.  &lt;br&gt;
&amp;gt; &amp;gt; While each data set tends to have the same number of &lt;br&gt;
&amp;gt; &amp;gt; columns, thus far I need to manually view 'colheader' &lt;br&gt;
&amp;gt; data &lt;br&gt;
&amp;gt; &amp;gt; string from each file in the data set to be certain &lt;br&gt;
the &lt;br&gt;
&amp;gt; &amp;gt; target columns are indeed the same column number.  &lt;br&gt;
Once I &lt;br&gt;
&amp;gt; &amp;gt; am certain of the column number I load this column &lt;br&gt;
into a &lt;br&gt;
&amp;gt; &amp;gt; seperate vector to use in other Matlab operations. Any &lt;br&gt;
&amp;gt; &amp;gt; suggestions on how can I determine the column number? &lt;br&gt;
Can &lt;br&gt;
&amp;gt; &amp;gt; I use the column headers (the 'colheader' string) to &lt;br&gt;
&amp;gt; &amp;gt; locate the specific column numbers?  &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Hi,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; we need more information about your colheader string, &lt;br&gt;
but &lt;br&gt;
&amp;gt; here's a hint to an automation routine that might work, &lt;br&gt;
as &lt;br&gt;
&amp;gt; a best guess:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; You know the exact distinct column title strings &lt;br&gt;
appearing &lt;br&gt;
&amp;gt; in your header line string&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;     myColumnNames = {'Val1','Val2','Val3','Val4'};&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; If your header string contains more than one line, you &lt;br&gt;
&amp;gt; should extract the line that contains your column names. &lt;br&gt;
&amp;gt; Maybe you can identify the lines by the positions of a &lt;br&gt;
line &lt;br&gt;
&amp;gt; break character:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;     lineBreakPos = strfind(colheader,char(10));&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Find out somehow which line has the names, perhaps you &lt;br&gt;
know &lt;br&gt;
&amp;gt; it beforehand. Assuming the k-th line is the line of &lt;br&gt;
&amp;gt; interest, do something like:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;     columnNameLine = colheader(lineBreakPos(k-1)+1: &lt;br&gt;
&amp;gt; lineBreakPos(k)-1 );&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; (take obvious precautions if it is the first or last &lt;br&gt;
line &lt;br&gt;
&amp;gt; in colheader)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Most probably there is a distinct delimiter (Tab, &lt;br&gt;
&amp;gt; space, ';', ',' ...) between the column name strings. &lt;br&gt;
Find &lt;br&gt;
&amp;gt; its positions in the line ...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;     delim = ';';&lt;br&gt;
&amp;gt;     delimPos = strfind(columnNameLine,delim);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; find out the positions of your column name strings ...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;     namePos(n) = strfind(columnNameLine,myColumnNames&lt;br&gt;
(n));&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; and compare them to the delimiter positions to make out &lt;br&gt;
the &lt;br&gt;
&amp;gt; corresponding column index of your data matrix.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I hope this gets you started.&lt;br&gt;
&amp;gt; Best regards&lt;br&gt;
&amp;gt; Andres&lt;br&gt;
&lt;br&gt;
Andres,&lt;br&gt;
thanks for your reply.&lt;br&gt;
This is probably too much detail but here it is:&lt;br&gt;
The original .csv file contains about 23 header lines, but &lt;br&gt;
I delete these so the first line is, infact, the column &lt;br&gt;
names (extra header lines will not import properly).  The &lt;br&gt;
file is then imported into Matlab and then save as a &lt;br&gt;
matrix.  Usually when I import csv files I have a matrix &lt;br&gt;
and 2 'arrays', one called &quot;textdata&quot; and the other &lt;br&gt;
called &quot;colheaders&quot;.  In this case one array was the &lt;br&gt;
actual column names/headers (e.g. 'Torque') and the other &lt;br&gt;
was the units for the column headers (e.g. 'Nm').  For &lt;br&gt;
some reason this is not working and I have to re-import &lt;br&gt;
the column headers as its own array - but this isn't &lt;br&gt;
really an issue.&lt;br&gt;
&lt;br&gt;
A better example:&lt;br&gt;
I have just loaded a data file with a matrix &lt;br&gt;
called &quot;datafile1&quot; (sized at &amp;lt;148x482 double&amp;gt;), and the &lt;br&gt;
corresponding column names is in &quot;colheaders&quot; array (size &lt;br&gt;
&amp;lt;1x482 cell&amp;gt;). I know the exact column names, but I must &lt;br&gt;
open the colheaders array and scroll to find where a &lt;br&gt;
target column is, since the data matrix doesn't have &lt;br&gt;
column names.&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;For example: the column name/header &quot;Torque&quot; is in &lt;br&gt;
column # 422 of the 'colheader' array. The corresponding &lt;br&gt;
torque data is in the matrix &quot;datafile1&quot; column no 422. &lt;br&gt;
The end goal is to use this column number (422) to read &lt;br&gt;
the Torque data into a seperate vector to be further &lt;br&gt;
manipulated later. If I double click on the colheaders &lt;br&gt;
cell named 'Torque', the array editor title is colheaders&lt;br&gt;
{1,422}.   &lt;br&gt;
&lt;br&gt;
Herein lies the problem.  I have to verify all files in &lt;br&gt;
this data set have the 'Torque' data stored in column 422 -&lt;br&gt;
or determine which column it is stored in.  Once &lt;br&gt;
determined, the torque data from each file will be saved &lt;br&gt;
as a seperate variable.  If I only had to manual verify &lt;br&gt;
this in 1 data set (approx 25 files), it would be okay.  &lt;br&gt;
Currently however, it is 125 files, with more on the way. &lt;br&gt;
&lt;br&gt;
Also: I believe the string data in column headers is &lt;br&gt;
delimited by single quote (') not semicolon (;).  So &lt;br&gt;
modifing the code to &quot; delim = '''; &quot; does not work.  Am &lt;br&gt;
also unsuccessful with strfind:&lt;br&gt;
&lt;br&gt;
strfind(colheaders,'Torque')&lt;br&gt;
ans = &lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;Columns 1 through 9&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[]     []     []  ...&lt;br&gt;
&amp;nbsp;&amp;nbsp;Columns 442 through 450&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[]     []     []     []     []&lt;br&gt;
etc.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Hope this helps expain what is going on.  Thanks so much &lt;br&gt;
for all your time.</description>
    </item>
    <item>
      <pubDate>Wed, 13 Aug 2008 07:58:02 -0400</pubDate>
      <title>Re: determine col num from column headers</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/174153#448855</link>
      <author>Andres </author>
      <description>&quot;Keli &quot; &amp;lt;kalark@ford.com&amp;gt; wrote in message &amp;lt;g7sq09$rj2&lt;br&gt;
$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
[..]&lt;br&gt;
&amp;gt; A better example:&lt;br&gt;
&amp;gt; I have just loaded a data file with a matrix &lt;br&gt;
&amp;gt; called &quot;datafile1&quot; (sized at &amp;lt;148x482 double&amp;gt;), and the &lt;br&gt;
&amp;gt; corresponding column names is in &quot;colheaders&quot; array (size &lt;br&gt;
&amp;gt; &amp;lt;1x482 cell&amp;gt;). I know the exact column names, but I must &lt;br&gt;
&amp;gt; open the colheaders array and scroll to find where a &lt;br&gt;
&amp;gt; target column is, since the data matrix doesn't have &lt;br&gt;
&amp;gt; column names.&lt;br&gt;
&amp;gt;    For example: the column name/header &quot;Torque&quot; is in &lt;br&gt;
&amp;gt; column # 422 of the 'colheader' array. The corresponding &lt;br&gt;
&amp;gt; torque data is in the matrix &quot;datafile1&quot; column no 422. &lt;br&gt;
&amp;gt; The end goal is to use this column number (422) to read &lt;br&gt;
&amp;gt; the Torque data into a seperate vector to be further &lt;br&gt;
&amp;gt; manipulated later. If I double click on the colheaders &lt;br&gt;
&amp;gt; cell named 'Torque', the array editor title is colheaders&lt;br&gt;
&amp;gt; {1,422}.   &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Herein lies the problem.  I have to verify all files in &lt;br&gt;
&amp;gt; this data set have the 'Torque' data stored in column &lt;br&gt;
422 -&lt;br&gt;
&amp;gt; or determine which column it is stored in.  Once &lt;br&gt;
&amp;gt; determined, the torque data from each file will be saved &lt;br&gt;
&amp;gt; as a seperate variable.  If I only had to manual verify &lt;br&gt;
&amp;gt; this in 1 data set (approx 25 files), it would be okay.  &lt;br&gt;
&amp;gt; Currently however, it is 125 files, with more on the way. &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Also: I believe the string data in column headers is &lt;br&gt;
&amp;gt; delimited by single quote (') not semicolon (;).  So &lt;br&gt;
&amp;gt; modifing the code to &quot; delim = '''; &quot; does not work.  Am &lt;br&gt;
&amp;gt; also unsuccessful with strfind:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; strfind(colheaders,'Torque')&lt;br&gt;
&amp;gt; ans = &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;   Columns 1 through 9&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;      []     []     []  ...&lt;br&gt;
&amp;gt;   Columns 442 through 450&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;      []     []     []     []     []&lt;br&gt;
&amp;gt; etc.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Hope this helps expain what is going on.  Thanks so much &lt;br&gt;
&amp;gt; for all your time.&lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
So your colheaders variable is a cell array of strings. &lt;br&gt;
Then I'd expect a non-empty element somewhere in the &lt;br&gt;
resulting cell array of strfind. &lt;br&gt;
Besides, use find(strcmp( rather than strfind( on the cell &lt;br&gt;
array. Perhaps you have to care about some extra whitespace &lt;br&gt;
around your column names.&lt;br&gt;
The following example works:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;colheaders = {' Freq','  Torque ','Temp '};&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cleanColheaders = strtrim(colheaders);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;idx = find(strcmp('Torque',cleanColheaders));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;% idx = 2&lt;br&gt;
&lt;br&gt;
Check if you need to use strcmpi instead of strcmp.&lt;br&gt;
Does this work for you as well? If not, can you find out &lt;br&gt;
why?</description>
    </item>
    <item>
      <pubDate>Thu, 14 Aug 2008 13:32:10 -0400</pubDate>
      <title>Re: determine col num from column headers</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/174153#449105</link>
      <author>Keli </author>
      <description>Fantastic! Thanks very much!&lt;br&gt;
&lt;br&gt;
Kinda Regards,&lt;br&gt;
Keli&lt;br&gt;
&lt;br&gt;
&amp;gt; [..]&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; So your colheaders variable is a cell array of strings. &lt;br&gt;
&amp;gt; Then I'd expect a non-empty element somewhere in the &lt;br&gt;
&amp;gt; resulting cell array of strfind. &lt;br&gt;
&amp;gt; Besides, use find(strcmp( rather than strfind( on the &lt;br&gt;
cell &lt;br&gt;
&amp;gt; array. Perhaps you have to care about some extra &lt;br&gt;
whitespace &lt;br&gt;
&amp;gt; around your column names.&lt;br&gt;
&amp;gt; The following example works:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;     colheaders = {' Freq','  Torque ','Temp '};&lt;br&gt;
&amp;gt;     cleanColheaders = strtrim(colheaders);&lt;br&gt;
&amp;gt;     idx = find(strcmp('Torque',cleanColheaders));&lt;br&gt;
&amp;gt;     % idx = 2&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Check if you need to use strcmpi instead of strcmp.&lt;br&gt;
&amp;gt; Does this work for you as well? If not, can you find out &lt;br&gt;
&amp;gt; why?</description>
    </item>
  </channel>
</rss>

