<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238111</link>
    <title>MATLAB Central Newsreader - read multi-line data with multiple delimiters</title>
    <description>Feed for thread: read multi-line data with multiple delimiters</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>Fri, 24 Oct 2008 15:04:27 -0400</pubDate>
      <title>read multi-line data with multiple delimiters</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238111#607191</link>
      <author>boivie@gmail.com</author>
      <description>Hi all - I've scoured the net and mathworks and haven't come across&lt;br&gt;
how to solve this.  I haven't used Matlab in quite a while, and it&lt;br&gt;
seems like this shouldn't be very hard, but I haven't managed to get&lt;br&gt;
this yet.&lt;br&gt;
&lt;br&gt;
The file I am reading in has data (after several file header lines)&lt;br&gt;
that consists of one line of header info (&quot;Line&quot; or &quot;Tie&quot; and the&lt;br&gt;
number - both of which I need to extract) and two lines of comma&lt;br&gt;
delimited data associated with that header.  I seem to be able to&lt;br&gt;
extract the first line of data using textscan with comma as the&lt;br&gt;
delimiter.  Using regexp, I have gotten both lines of data, but as 4&lt;br&gt;
variables, rather than 8.  Any pointers or code help would be much&lt;br&gt;
appreciated.  Thanks.&lt;br&gt;
&lt;br&gt;
-Ethan&lt;br&gt;
&lt;br&gt;
This is what the file looks like:&lt;br&gt;
&lt;br&gt;
/Data&lt;br&gt;
/X,Y,Longitude,Latitude&lt;br&gt;
//Flight 0&lt;br&gt;
//Date 2008/09/27&lt;br&gt;
Line  0&lt;br&gt;
-450.0,50.0,147.5445363050,-76.4478227596&lt;br&gt;
450.0,50.0,182.4554636951,-76.4478227596&lt;br&gt;
Line  10&lt;br&gt;
-450.0,-50.0,146.3500677843,-77.3018107353&lt;br&gt;
450.0,-50.0,183.6499322158,-77.3018107353&lt;br&gt;
Tie  -10&lt;br&gt;
-360.0,-100.0,149.3523576944,-77.9633702276&lt;br&gt;
-360.0,100.0,151.3351657935,-76.2267188392&lt;br&gt;
Tie  0&lt;br&gt;
-160.0,-100.0,157.9105218900,-78.3092770253&lt;br&gt;
-160.0,100.0,158.8372826671,-76.5286269834</description>
    </item>
    <item>
      <pubDate>Fri, 24 Oct 2008 15:23:07 -0400</pubDate>
      <title>Re: read multi-line data with multiple delimiters</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238111#607197</link>
      <author>Rune Allnor</author>
      <description>On 24 Okt, 17:04, boi...@gmail.com wrote:&lt;br&gt;
&amp;gt; Hi all - I've scoured the net and mathworks and haven't come across&lt;br&gt;
&amp;gt; how to solve this. =A0I haven't used Matlab in quite a while, and it&lt;br&gt;
&amp;gt; seems like this shouldn't be very hard, but I haven't managed to get&lt;br&gt;
&amp;gt; this yet.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; The file I am reading in has data (after several file header lines)&lt;br&gt;
&amp;gt; that consists of one line of header info (&quot;Line&quot; or &quot;Tie&quot; and the&lt;br&gt;
&amp;gt; number - both of which I need to extract) and two lines of comma&lt;br&gt;
&amp;gt; delimited data associated with that header. =A0I seem to be able to&lt;br&gt;
&amp;gt; extract the first line of data using textscan with comma as the&lt;br&gt;
&amp;gt; delimiter. =A0Using regexp, I have gotten both lines of data, but as 4&lt;br&gt;
&amp;gt; variables, rather than 8. =A0Any pointers or code help would be much&lt;br&gt;
&amp;gt; appreciated. =A0Thanks.&lt;br&gt;
&lt;br&gt;
It seems you are almost there. REGEXP is the method&lt;br&gt;
I would use to detect the header lines. However,&lt;br&gt;
you might want to use SSCANF to read the two data lines.&lt;br&gt;
Just remember that you read each data line one at the&lt;br&gt;
time and need to store the corresponding data somewhere.&lt;br&gt;
&lt;br&gt;
The basic program would go something like (pseudocode!)&lt;br&gt;
&lt;br&gt;
while (~feof(fid))&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;while (~regexpmatch('tie'|'line'))&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;s =3D fgetl(fid)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;extract number from present line that contains&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;'tie' or 'line'&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;s =3D fgetl(fid)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;A =3D sscanf(s,'%f,%f,%f,%f');&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;s =3D fgetl(fid)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;B =3D sscanf(s,'%f,%f,%f,%f');&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;store A and B wherever they need to go&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
Rune</description>
    </item>
    <item>
      <pubDate>Fri, 24 Oct 2008 19:43:28 -0400</pubDate>
      <title>Re: read multi-line data with multiple delimiters</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238111#607234</link>
      <author>boivie@gmail.com</author>
      <description>On Oct 24, 11:23=A0am, Rune Allnor &amp;lt;all...@tele.ntnu.no&amp;gt; wrote:&lt;br&gt;
&amp;gt; On 24 Okt, 17:04, boi...@gmail.com wrote:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; Hi all - I've scoured the net and mathworks and haven't come across&lt;br&gt;
&amp;gt; &amp;gt; how to solve this. =A0I haven't used Matlab in quite a while, and it&lt;br&gt;
&amp;gt; &amp;gt; seems like this shouldn't be very hard, but I haven't managed to get&lt;br&gt;
&amp;gt; &amp;gt; this yet.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; The file I am reading in hasdata(after several file header lines)&lt;br&gt;
&amp;gt; &amp;gt; that consists of one line of header info (&quot;Line&quot; or &quot;Tie&quot; and the&lt;br&gt;
&amp;gt; &amp;gt; number - both of which I need to extract) and two lines of comma&lt;br&gt;
&amp;gt; &amp;gt; delimiteddataassociated with that header. =A0I seem to be able to&lt;br&gt;
&amp;gt; &amp;gt; extract the first line ofdatausing textscan with comma as the&lt;br&gt;
&amp;gt; &amp;gt; delimiter. =A0Using regexp, I have gotten both lines ofdata, but as 4&lt;br&gt;
&amp;gt; &amp;gt; variables, rather than 8. =A0Any pointers or code help would be much&lt;br&gt;
&amp;gt; &amp;gt; appreciated. =A0Thanks.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; It seems you are almost there. REGEXP is the method&lt;br&gt;
&amp;gt; I would use to detect the header lines. However,&lt;br&gt;
&amp;gt; you might want to use SSCANF to read the twodatalines.&lt;br&gt;
&amp;gt; Just remember that you read eachdataline one at the&lt;br&gt;
&amp;gt; time and need to store the correspondingdatasomewhere.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; The basic program would go something like (pseudocode!)&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; while (~feof(fid))&lt;br&gt;
&amp;gt; =A0 =A0while (~regexpmatch('tie'|'line'))&lt;br&gt;
&amp;gt; =A0 =A0 =A0 s =3D fgetl(fid)&lt;br&gt;
&amp;gt; =A0 =A0end&lt;br&gt;
&amp;gt; =A0 =A0extract number from present line that contains&lt;br&gt;
&amp;gt; =A0 =A0'tie' or 'line'&lt;br&gt;
&amp;gt; =A0 =A0s =3D fgetl(fid)&lt;br&gt;
&amp;gt; =A0 =A0A =3D sscanf(s,'%f,%f,%f,%f');&lt;br&gt;
&amp;gt; =A0 =A0s =3D fgetl(fid)&lt;br&gt;
&amp;gt; =A0 =A0B =3D sscanf(s,'%f,%f,%f,%f');&lt;br&gt;
&amp;gt; =A0 =A0store A and B wherever they need to go&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Rune&lt;br&gt;
&lt;br&gt;
Rune,&lt;br&gt;
&lt;br&gt;
Thanks very much for the help.  I'm getting an error using the OR&lt;br&gt;
operator '|' in this line: while (~regexpmatch('Tie'|'Line')).&lt;br&gt;
Matlab returns:&lt;br&gt;
&amp;nbsp;&amp;nbsp;'Error using =3D=3D&amp;gt; or&lt;br&gt;
&amp;nbsp;&amp;nbsp;Inputs must have the same size.'&lt;br&gt;
&lt;br&gt;
I tried a few things, including using '||' instead of '|', and&lt;br&gt;
breaking it up into two parts: 'while (~regexpmatch('Tie') &amp;&amp;&lt;br&gt;
~regexpmatch('Line'))'&lt;br&gt;
&lt;br&gt;
Neither has worked.  Any suggestions?  Thanks.&lt;br&gt;
&lt;br&gt;
Ethan</description>
    </item>
    <item>
      <pubDate>Fri, 24 Oct 2008 19:48:02 -0400</pubDate>
      <title>read multi-line data with multiple delimiters</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238111#607237</link>
      <author>Andres </author>
      <description>boivie@gmail.com wrote in message &amp;lt;70d4e42d-48e0-4f97-a9d2-83aa4eeed8c2@l76g2000hse.googlegroups.com&amp;gt;...&lt;br&gt;
[..]&lt;br&gt;
&amp;gt; This is what the file looks like:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; /Data&lt;br&gt;
&amp;gt; /X,Y,Longitude,Latitude&lt;br&gt;
&amp;gt; //Flight 0&lt;br&gt;
&amp;gt; //Date 2008/09/27&lt;br&gt;
&amp;gt; Line  0&lt;br&gt;
&amp;gt; -450.0,50.0,147.5445363050,-76.4478227596&lt;br&gt;
&amp;gt; 450.0,50.0,182.4554636951,-76.4478227596&lt;br&gt;
&amp;gt; Line  10&lt;br&gt;
&amp;gt; -450.0,-50.0,146.3500677843,-77.3018107353&lt;br&gt;
&amp;gt; 450.0,-50.0,183.6499322158,-77.3018107353&lt;br&gt;
&amp;gt; Tie  -10&lt;br&gt;
&amp;gt; -360.0,-100.0,149.3523576944,-77.9633702276&lt;br&gt;
&amp;gt; -360.0,100.0,151.3351657935,-76.2267188392&lt;br&gt;
&amp;gt; Tie  0&lt;br&gt;
&amp;gt; -160.0,-100.0,157.9105218900,-78.3092770253&lt;br&gt;
&amp;gt; -160.0,100.0,158.8372826671,-76.5286269834&lt;br&gt;
&lt;br&gt;
Hi Ethan,&lt;br&gt;
if it is ok that your 'Line' or 'Tie' flags are represented as 0 or 1 in a double array, there is also a (more convenient?) single command option with txt2mat from the file exchange:&lt;br&gt;
&lt;br&gt;
A = txt2mat('c:file.txt','ReplaceExpr',{{'Line','0'},{'Tie','1'}});&lt;br&gt;
&lt;br&gt;
The header lines and delimiters are detected automatically (see the doc), and the matrix is padded with NaNs to fill up the rows.&lt;br&gt;
&lt;br&gt;
With your example, A would be&lt;br&gt;
&lt;br&gt;
0	0	NaN	NaN&lt;br&gt;
-450	50	147.5445	-76.4478&lt;br&gt;
450	50	182.4555	-76.4478&lt;br&gt;
0	10	NaN	NaN&lt;br&gt;
-450	-50	146.3501	-77.3018&lt;br&gt;
450	-50	183.6499	-77.3018&lt;br&gt;
1	-10	NaN	NaN&lt;br&gt;
-360	-100	149.3524	-77.9634&lt;br&gt;
-360	100	151.3352	-76.2267&lt;br&gt;
1	0	NaN	NaN&lt;br&gt;
-160	-100	157.9105	-78.3093&lt;br&gt;
-160	100	158.8373	-76.5286&lt;br&gt;
&lt;br&gt;
(short notation just for the ng)&lt;br&gt;
&lt;br&gt;
Hth,&lt;br&gt;
regards,&lt;br&gt;
Andres</description>
    </item>
    <item>
      <pubDate>Fri, 24 Oct 2008 20:26:14 -0400</pubDate>
      <title>Re: read multi-line data with multiple delimiters</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238111#607247</link>
      <author>Walter Roberson</author>
      <description>boivie@gmail.com wrote:&lt;br&gt;
&amp;gt; while (~regexpmatch('Tie'|'Line')).&lt;br&gt;
&lt;br&gt;
isempty(regexp(s, '^Tie|^Line', 'once'))&lt;br&gt;
&lt;br&gt;
and you will have to adjust Rune's pseudo-code to read a line&lt;br&gt;
into s at the beginning.</description>
    </item>
  </channel>
</rss>

