<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239003</link>
    <title>MATLAB Central Newsreader - ascii import</title>
    <description>Feed for thread: ascii import</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, 10 Nov 2008 19:32:02 -0500</pubDate>
      <title>ascii import</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239003#610131</link>
      <author>Zahra </author>
      <description>Hi all,&lt;br&gt;
&lt;br&gt;
Can any one help for the following:&lt;br&gt;
&lt;br&gt;
I need to import data from an ascii file. The file includes different data runs that are separated by a header (the text of the header changes from one run to the next but the number of header lines remain the same for different runs). Then each run has the same number of data points. The only problem is that for each data point, the data is given in two lines (there is a line folding) starting with the point number in the run. Here is an example of the data file, lets assume there are two runs in it and each run has two points:&lt;br&gt;
--------------------------------&lt;br&gt;
Run   1   &lt;br&gt;
header line 1&lt;br&gt;
header line 2&lt;br&gt;
Point1   v1   v2   v3   v4 &lt;br&gt;
Point1   v5   v6 &lt;br&gt;
Point2   v1'   v2'   v3'   v4' &lt;br&gt;
Point2   v5'   v6' &lt;br&gt;
&lt;br&gt;
Run  2   &lt;br&gt;
header line 1&lt;br&gt;
header line 2&lt;br&gt;
Point1   vv1   vv2   vv3   vv4 &lt;br&gt;
Point1   vv5   vv6 &lt;br&gt;
Point2   vv1'   vv2'   vv3'   vv4' &lt;br&gt;
Point2   vv5'   vv6'&lt;br&gt;
-------------------------------------------&lt;br&gt;
and so on...&lt;br&gt;
&lt;br&gt;
What I need is to remove all the header lines and have a matrix that each of its rows represents a data point and its column are the data values, i.e. the rows of the final matrix need to be in the following format:&lt;br&gt;
&lt;br&gt;
row 1=[Point1   v1   v2   v3   v4   v5   v6]&lt;br&gt;
row 2=[Point2   v1'   v2'   v3'   v4'   v5'   v6']&lt;br&gt;
row 3=[Point1   vv1   vv2   vv3   vv4   vv5   vv6]&lt;br&gt;
row 4=[Point2   vv1'   vv2'   vv3'   vv4'   vv5'   vv6']&lt;br&gt;
&lt;br&gt;
I have tried different options in txt2mat, but with each option there is one problem or another.&lt;br&gt;
&lt;br&gt;
Can any one help?&lt;br&gt;
&lt;br&gt;
Thanks,&lt;br&gt;
Zahra</description>
    </item>
    <item>
      <pubDate>Mon, 10 Nov 2008 20:24:14 -0500</pubDate>
      <title>Re: ascii import</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239003#610143</link>
      <author>NZTideMan</author>
      <description>On Nov 11, 8:32=A0am, &quot;Zahra&quot; &amp;lt;zahra.yam...@nrc.gc.ca&amp;gt; wrote:&lt;br&gt;
&amp;gt; Hi all,&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Can any one help for the following:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; I need to import data from an ascii file. The file includes different dat=&lt;br&gt;
a runs that are separated by a header (the text of the header changes from =&lt;br&gt;
one run to the next but the number of header lines remain the same for diff=&lt;br&gt;
erent runs). Then each run has the same number of data points. The only pro=&lt;br&gt;
blem is that for each data point, the data is given in two lines (there is =&lt;br&gt;
a line folding) starting with the point number in the run. Here is an examp=&lt;br&gt;
le of the data file, lets assume there are two runs in it and each run has =&lt;br&gt;
two points:&lt;br&gt;
&amp;gt; --------------------------------&lt;br&gt;
&amp;gt; Run =A0 1 =A0&lt;br&gt;
&amp;gt; header line 1&lt;br&gt;
&amp;gt; header line 2&lt;br&gt;
&amp;gt; Point1 =A0 v1 =A0 v2 =A0 v3 =A0 v4&lt;br&gt;
&amp;gt; Point1 =A0 v5 =A0 v6&lt;br&gt;
&amp;gt; Point2 =A0 v1' =A0 v2' =A0 v3' =A0 v4'&lt;br&gt;
&amp;gt; Point2 =A0 v5' =A0 v6'&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Run =A02 =A0&lt;br&gt;
&amp;gt; header line 1&lt;br&gt;
&amp;gt; header line 2&lt;br&gt;
&amp;gt; Point1 =A0 vv1 =A0 vv2 =A0 vv3 =A0 vv4&lt;br&gt;
&amp;gt; Point1 =A0 vv5 =A0 vv6&lt;br&gt;
&amp;gt; Point2 =A0 vv1' =A0 vv2' =A0 vv3' =A0 vv4'&lt;br&gt;
&amp;gt; Point2 =A0 vv5' =A0 vv6'&lt;br&gt;
&amp;gt; -------------------------------------------&lt;br&gt;
&amp;gt; and so on...&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; What I need is to remove all the header lines and have a matrix that each=&lt;br&gt;
&amp;nbsp;of its rows represents a data point and its column are the data values, i.=&lt;br&gt;
e. the rows of the final matrix need to be in the following format:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; row 1=3D[Point1 =A0 v1 =A0 v2 =A0 v3 =A0 v4 =A0 v5 =A0 v6]&lt;br&gt;
&amp;gt; row 2=3D[Point2 =A0 v1' =A0 v2' =A0 v3' =A0 v4' =A0 v5' =A0 v6']&lt;br&gt;
&amp;gt; row 3=3D[Point1 =A0 vv1 =A0 vv2 =A0 vv3 =A0 vv4 =A0 vv5 =A0 vv6]&lt;br&gt;
&amp;gt; row 4=3D[Point2 =A0 vv1' =A0 vv2' =A0 vv3' =A0 vv4' =A0 vv5' =A0 vv6']&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; I have tried different options in txt2mat, but with each option there is =&lt;br&gt;
one problem or another.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Can any one help?&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Thanks,&lt;br&gt;
&amp;gt; Zahra&lt;br&gt;
&lt;br&gt;
I'm not familiar with txt2mat, but this is what I'd do:&lt;br&gt;
&lt;br&gt;
fid=3Dfopen(txtfile,'rt');&lt;br&gt;
A=3Dzeros(16,nruns);     % Allocate storage for the data&lt;br&gt;
for irun=3D1:nruns    % Loop thru the runs&lt;br&gt;
&amp;nbsp;&amp;nbsp;Run{irun}=3Dfgetl(fid);&lt;br&gt;
&amp;nbsp;&amp;nbsp;header1{irun}=3Dfgetl(fid);&lt;br&gt;
&amp;nbsp;&amp;nbsp;header2{irun}=3Dfgetl(fid);&lt;br&gt;
&amp;nbsp;&amp;nbsp;A(:,irun)=3Dfscanf(fid,'%f',16);   % read in the 16 data from each run&lt;br&gt;
&amp;nbsp;&amp;nbsp;dum=3Dfgetl(fid);    % read in blank line and discard&lt;br&gt;
end&lt;br&gt;
fclose(fid);&lt;br&gt;
&lt;br&gt;
Now you have to manipulate A into the form you want by eliminating&lt;br&gt;
unnecessary rows, then transposing and reshaping.</description>
    </item>
    <item>
      <pubDate>Mon, 10 Nov 2008 20:44:01 -0500</pubDate>
      <title>Re: ascii import</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239003#610152</link>
      <author>Andres </author>
      <description>&quot;Zahra&quot; &amp;lt;zahra.yamani@nrc.gc.ca&amp;gt; wrote in message &amp;lt;gfa27i$kr6$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Hi all,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Can any one help for the following:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I need to import data from an ascii file. The file includes different data runs that are separated by a header (the text of the header changes from one run to the next but the number of header lines remain the same for different runs). Then each run has the same number of data points. The only problem is that for each data point, the data is given in two lines (there is a line folding) starting with the point number in the run. Here is an example of the data file, lets assume there are two runs in it and each run has two points:&lt;br&gt;
&amp;gt; --------------------------------&lt;br&gt;
&amp;gt; Run   1   &lt;br&gt;
&amp;gt; header line 1&lt;br&gt;
&amp;gt; header line 2&lt;br&gt;
&amp;gt; Point1   v1   v2   v3   v4 &lt;br&gt;
&amp;gt; Point1   v5   v6 &lt;br&gt;
&amp;gt; Point2   v1'   v2'   v3'   v4' &lt;br&gt;
&amp;gt; Point2   v5'   v6' &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Run  2   &lt;br&gt;
&amp;gt; header line 1&lt;br&gt;
&amp;gt; header line 2&lt;br&gt;
&amp;gt; Point1   vv1   vv2   vv3   vv4 &lt;br&gt;
&amp;gt; Point1   vv5   vv6 &lt;br&gt;
&amp;gt; Point2   vv1'   vv2'   vv3'   vv4' &lt;br&gt;
&amp;gt; Point2   vv5'   vv6'&lt;br&gt;
&amp;gt; -------------------------------------------&lt;br&gt;
&amp;gt; and so on...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; What I need is to remove all the header lines and have a matrix that each of its rows represents a data point and its column are the data values, i.e. the rows of the final matrix need to be in the following format:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; row 1=[Point1   v1   v2   v3   v4   v5   v6]&lt;br&gt;
&amp;gt; row 2=[Point2   v1'   v2'   v3'   v4'   v5'   v6']&lt;br&gt;
&amp;gt; row 3=[Point1   vv1   vv2   vv3   vv4   vv5   vv6]&lt;br&gt;
&amp;gt; row 4=[Point2   vv1'   vv2'   vv3'   vv4'   vv5'   vv6']&lt;br&gt;
&amp;gt; &lt;br&gt;
.&lt;br&gt;
Let's make a test file:&lt;br&gt;
.&lt;br&gt;
Run 1&lt;br&gt;
header line 1&lt;br&gt;
header line 2&lt;br&gt;
Point1 1 2 3 4&lt;br&gt;
Point1 5 6&lt;br&gt;
Point2 1 2 3 7&lt;br&gt;
Point2 5 7&lt;br&gt;
Run 2&lt;br&gt;
header line 1&lt;br&gt;
header line 2&lt;br&gt;
Point1 1 2 3 8&lt;br&gt;
Point1 5 8&lt;br&gt;
Point2 1 2 3 9&lt;br&gt;
Point2 5 9&lt;br&gt;
.&lt;br&gt;
Two txt2mat alternatives out of the box:&lt;br&gt;
Case 1)&lt;br&gt;
each interim header line can be identified by a set of key phrases, here:&lt;br&gt;
'Run' and 'header'&lt;br&gt;
.&lt;br&gt;
t2mOptions = {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'NumHeaderLines',3, ...&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'NumColumns',8, ...&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'ReplaceExpr',{{'Point','    '}}, ...&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'BadLineString',{'Run','header'}, ...&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'ReadMode','block', ...&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;};&lt;br&gt;
A = txt2mat('zahra.txt',t2mOptions{:});&lt;br&gt;
.&lt;br&gt;
gives:&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1     1     2     3     4     1     5     6&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;2     1     2     3     7     2     5     7&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1     1     2     3     8     1     5     8&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;2     1     2     3     9     2     5     9&lt;br&gt;
Note that the point number is repeated in the 6th column, you'll probably want to delete this column.&lt;br&gt;
.&lt;br&gt;
Case 2) &lt;br&gt;
the interim header lines can be not identified by any special phrase or character&lt;br&gt;
.&lt;br&gt;
t2mOptions = {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'NumHeaderLines',3, ...&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'NumColumns',-1, ...&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'ReplaceExpr',{{'Point','    '}}, ...&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'ReadMode','line', ...&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;};&lt;br&gt;
B = txt2mat('zahra.txt',t2mOptions{:});&lt;br&gt;
.&lt;br&gt;
gives&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1     1     2     3     4&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1     5     6   NaN   NaN&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;2     1     2     3     7&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;2     5     7   NaN   NaN&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;NaN   NaN   NaN   NaN   NaN&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;NaN   NaN   NaN   NaN   NaN&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;NaN   NaN   NaN   NaN   NaN&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1     1     2     3     8&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1     5     8   NaN   NaN&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;2     1     2     3     9&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;2     5     9   NaN   NaN&lt;br&gt;
.&lt;br&gt;
this obviously needs further post-processing with elementary matrix indexing operations, but all your numbers are there.&lt;br&gt;
.&lt;br&gt;
I hope you can adopt this to your problem. There are many possibilities without txt2mat of course - as I just read one above.&lt;br&gt;
Regards&lt;br&gt;
Andres</description>
    </item>
    <item>
      <pubDate>Mon, 10 Nov 2008 21:30:03 -0500</pubDate>
      <title>Re: ascii import</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239003#610162</link>
      <author>Zahra </author>
      <description>Hi Andreas,&lt;br&gt;
&lt;br&gt;
Thanks for your reply.&lt;br&gt;
&lt;br&gt;
I should have been more precise in defining my data files. They actually look like:&lt;br&gt;
--------------------------&lt;br&gt;
&lt;br&gt;
Run 1&lt;br&gt;
header line 1&lt;br&gt;
header line 2&lt;br&gt;
&lt;br&gt;
Point zeta eta gamma delta&lt;br&gt;
Point sig1 sig2&lt;br&gt;
1 1 2 3 4&lt;br&gt;
1 5 6&lt;br&gt;
2 1 2 3 7&lt;br&gt;
2 5 7&lt;br&gt;
&lt;br&gt;
Run 2&lt;br&gt;
header line 1&lt;br&gt;
header line 2&lt;br&gt;
&lt;br&gt;
Point zeta eta gamma delta&lt;br&gt;
Point sig1 sig2&lt;br&gt;
1 1 2 3 8&lt;br&gt;
1 5 8&lt;br&gt;
2 1 2 3 9&lt;br&gt;
2 5 9&lt;br&gt;
---------------------------------&lt;br&gt;
&lt;br&gt;
i.e. the word &quot;Point&quot; is not repeated at the begining of each data point just in the header line, instead the actual points in the run are recorded as shown above. Will it stil be possible to make the data matrix similar to the data file that you had assumed?&lt;br&gt;
&lt;br&gt;
Thanks again.&lt;br&gt;
Zahra</description>
    </item>
    <item>
      <pubDate>Tue, 11 Nov 2008 08:31:02 -0500</pubDate>
      <title>Re: ascii import</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239003#610229</link>
      <author>Andres </author>
      <description>This should just make things easier.&lt;br&gt;
- 1st attempt&lt;br&gt;
you can identify the header lines (e.g. by character 'n' as in 'Run','Line','Point' who must not appear inside the number lines) and you know how many numbers you want to put into a single row of the matrix (8):&lt;br&gt;
.&lt;br&gt;
t2mOptions = {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'NumHeaderLines',5, ... % 0 would be ok, too&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'NumColumns',8, ...&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'BadLineString',{'n'}, ... % or e.g. {'Run','Line','Point' }&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'ReadMode','block', ...&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;};&lt;br&gt;
A = txt2mat('zahra2.txt',t2mOptions{:});&lt;br&gt;
.&lt;br&gt;
gives&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1     1     2     3     4     1     5     6&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;2     1     2     3     7     2     5     7&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1     1     2     3     8     1     5     8&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;2     1     2     3     9     2     5     9&lt;br&gt;
.&lt;br&gt;
You may have to modify this according to your specific file.&lt;br&gt;
.&lt;br&gt;
- 2nd attempt (more general, but a bit slower) - you just know you want to combine two consecutive rows:&lt;br&gt;
t2mOptions = {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'NumHeaderLines',5, ... % 0 would be ok, too&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'NumColumns',-1, ...&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'ReadMode','line', ...&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;};&lt;br&gt;
B = txt2mat('zahra2.txt',t2mOptions{:});&lt;br&gt;
% now rearrange numbers:&lt;br&gt;
dataRow = find(isfinite(B(:,1)));&lt;br&gt;
B = B(dataRow,:);&lt;br&gt;
dataCol{1} = find(any(isfinite(B(1:2:end,:)),1));&lt;br&gt;
dataCol{2} = find(any(isfinite(B(2:2:end,:)),1));&lt;br&gt;
B = [B(1:2:end,dataCol{1}),B(2:2:end,dataCol{2})];&lt;br&gt;
.&lt;br&gt;
gives the same result for B. But note there must not be continous NaNs in one data position in your file, otherwise their column would be omitted.&lt;br&gt;
Hth&lt;br&gt;
Andres</description>
    </item>
    <item>
      <pubDate>Tue, 11 Nov 2008 13:38:02 -0500</pubDate>
      <title>Re: ascii import</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239003#610271</link>
      <author>Zahra </author>
      <description>Hi Andres,&lt;br&gt;
&lt;br&gt;
The second method that you described in your last message works perfectly with my data files which have complicated header lines. &lt;br&gt;
&lt;br&gt;
Indeed txt2mat.m is a powerful code.&lt;br&gt;
&lt;br&gt;
Thanks very much again for all your help.&lt;br&gt;
&lt;br&gt;
Zahra</description>
    </item>
  </channel>
</rss>

