<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/156217</link>
    <title>MATLAB Central Newsreader - Need File I/O help for reading and rewriting string data</title>
    <description>Feed for thread: Need File I/O help for reading and rewriting string data</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>Sun, 16 Sep 2007 02:42:18 -0400</pubDate>
      <title>Need File I/O help for reading and rewriting string data</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/156217#392479</link>
      <author> &quot;G.A.M.</author>
      <description>I need to read from a CSV file of mixed data types (mostly strings). I&lt;br&gt;
need to process each record (such as adding a few more values to it).&lt;br&gt;
Then I need to write (or append) the data to another CSV file.&lt;br&gt;
&lt;br&gt;
I can't seem to find a decent way to do this. The code below is super&lt;br&gt;
slow on large files (and this code is a drastic simplification of what&lt;br&gt;
I really need to do). I actually have to look for and remove duplicate&lt;br&gt;
records before writing out the modified data.&lt;br&gt;
&lt;br&gt;
I would appreciate any suggestions for improving the code. (For now&lt;br&gt;
I'm stuck using ASCII files, so I can't go to a database or binary&lt;br&gt;
files.)&lt;br&gt;
&lt;br&gt;
filedata = textread(myFilename, '%s','delimiter','\n');&lt;br&gt;
charRec = '';&lt;br&gt;
outputData = cell(length(filedata), 1);%preallocate&lt;br&gt;
for k = 1 : length(filedata)&lt;br&gt;
	record = strread(char(filedata(k)),'%s','delimiter',',');&lt;br&gt;
	for m = 1 : length (record)&lt;br&gt;
		temp = strcat(record{m}, ',');&lt;br&gt;
		charRec = strcat(charRec, temp);&lt;br&gt;
	end%for&lt;br&gt;
	charRec = strcat(charRec, 'newData');&lt;br&gt;
	outputData(k) = {charRec};&lt;br&gt;
	charRec = '';&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
testFile = fopen (testFilename,'wt');&lt;br&gt;
if testFile ~= -1&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for k = 1 : length(outputData)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;	fprintf(testFile,'%s\n', char(outputData(k)));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;fclose(testFile);&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
3 lines of sample data:&lt;br&gt;
lastname1,firstname,DOB,Male,Caucasian,,,sp = 0 ti = 26,22,ox(1),03-&lt;br&gt;
Aug-2007 10:36:48,13.15,5.85,189.058,18.9,5.8&lt;br&gt;
lastname2,firstname,DOB,Male,Caucasian,,,sp = 0 ti = 33,22,ox(2),03-&lt;br&gt;
Aug-2007 10:37:20,16.54,6.35,213.073,21.3,7.3&lt;br&gt;
lastname3,firstname,DOB,Male,Caucasian,,,sp = 0 ti = 27,22,ox(3),03-&lt;br&gt;
Aug-2007 10:53:16,15.86,7.68,192.082,19.2,8.2&lt;br&gt;
&lt;br&gt;
I hope all this code and data comes out readable.</description>
    </item>
  </channel>
</rss>

