Rewrite first 3 columns of csv without re-writing the entire file

2 views (last 30 days)
I have large csv files that need the first 3 columns rewritten. Is it possible to do that without rewritting the rest of the columns to save computing time. I will need to skipp the first two rows which are header strings and the first column that I am rewritting is a time/date string while the next two columns are numbers.
Here is what I have so far
FileList = dir('*.csv');
% get the file name:
filename = FileList(1).name
%%%%%%%%%Renames files according to date %%%%%%%%%%%
presg=filename(1:11);
mid='CORR';
sufsg=filename(12:end);
sufsg2=filename(12:26);
lf=strcat(presg,sufsg2);
load(lf);
% IIBOP_128HzCORR_2013_07_18_000.csv
newName=strcat(presg,mid,sufsg);
zipName=strcat(presg,mid,sufsg2);
movefile(filename,newName);
%%Display File Start Date/Time and End Date/Time
tcnt=length(DATA(:,1)); % total cnts HS***********
fstdate=datestr(DATA(1,1), 'mm/dd/yyyy HH:MM:SS:FFF')
fenddate=datestr(DATA(tcnt,1), 'mm/dd/yyyy HH:MM:SS:FFF')
%%Enter offsets (s)
rn1=0;
rn2=0;
rn3=0;
cp1=datenum('07/14/2013 20:40:02:445', 'mm/dd/yyyy HH:MM:SS:FFF');
os1=rn1*(1/(24*3600));
cp2=datenum('07/14/2013 20:40:03:203', 'mm/dd/yyyy HH:MM:SS:FFF');
os2=rn2*(1/(24*3600));
cp3=datenum('07/14/2013 20:40:03:203', 'mm/dd/yyyy HH:MM:SS:FFF');
os3=rn3*(1/(24*3600));
DATA2=zeros(tcnt,1:3);
DATA2(1:tcnt,2)=DATA(1:tcnt,2);
for i=1:tcnt;
if DATA(i,1)<=cp1;
DATA2(i,1)=DATA(i,1);
DATA2(i,3)=DATA(i,3);
else if DATA(i,1)>cp1 && DATA(i,1)<=cp2;
DATA2(i,1)=DATA(i,1)+os1;
DATA2(i,3)=DATA(i,3)+rn1;
else if DATA(i,1)>cp2 && DATA(i,1)<=cp3;
DATA2(i,1)=DATA(i,1)+os1+os2;
DATA2(i,3)=DATA(i,3)+rn1+rn2;
else
DATA2(i,1)=DATA(i,1)+os1+os2+os3;
DATA2(i,3)=DATA(i,3)+rn1+rn2+rn3;
end
end
end
end
samp=tcnt;
csvwrite(newName,DATA2,2,0);
zip(zipName,newName);
The original file was writen like this:
% Adate_string=datestr(xt(1:samp,1),'mm/dd/yyyy HH:MM:SS:FFF');
% Cdate=cellstr(Adate_string);
%
% Headers = {'Datetime','TimerTick','Relatvie_Time',...
% 'String_Torque','String_Weight','String_Pressure',...
% 'String_Acceleration_Z','String_Rotational_Velocity',...
% 'StringSense_Heart_Beat','StringSense_Voltage',...
% 'StringSense_Temperature','Node_RSSI','Base_RSSI'};
%
% FUnits = {'Datetime','Unitless','Seconds',...
% 'Kft-Lbs','KIP','Psi',...
% 'G','RPM',...
% 'Unitless','DC_Voltage',...
% 'C','dBm','dBm'};
% fid = fopen(newName, 'W'); % Uppercase, no text mode
% fprintf(fid, '%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\r\n', ...
% Headers{:}, FUnits{:});
%
%
% for i=1:samp;
% fprintf(fid, '%s,', Cdate{i,1});
% for cc = 1:12
% if cc<12
% fprintf(fid, '%.10f,', Data_EX(i, cc));
% else
% fprintf(fid, '%.10f\r\n', Data_EX(i, cc));
% end
% end
% end
% fclose(fid);
Here is the file formatt:
Datetime TimerTick Relatvie_Time String_Torque String_Weight String_Pressure String_Acceleration_Z String_Rotational_Velocity StringSense_Heart_Beat StringSense_Voltage StringSense_Temperature Node_RSSI Base_RSSI
Datetime Unitless Seconds Kft-Lbs KIP Psi G RPM Unitless DC_Voltage C dBm dBm
07/14/2013 20:40:02:445 16187 126.4609375 0.043643963 0.50814116 -76.4645462 0.062795378 1.104599714 0.998359978 6.136816025 37.9807663 -44 -51
07/14/2013 20:40:02:453 16188 126.46875 0.043643963 0.50814116 -71.68567657 0.05592538 1.104599714 0.998359978 6.136816025 37.9807663 -44 -49
Thank You,
Ian

Answers (1)

Walter Roberson
Walter Roberson on 23 Jan 2014
In the case where you are changing a string to another string of exactly the same size, it is possible to rewrite a file in place. This will require extra computation time and I/O operations compared to creating a new file with the changed data. You would not gain any benefit with lines that short.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!