Modifying files and merge files

2 Ansichten (letzte 30 Tage)
djr
djr am 1 Aug. 2014
Beantwortet: djr am 1 Aug. 2014
Hi,
I have a file that has dates in the folowing format:
1011949 % This would be 01JAN1949
2011949 % This would be 02JAN1949
11031949 % This would be 11MAR1949 (and so on... up to 2010).
I need to modify this array in a way to have:
1011949 0
1011949 6
1011949 12
1011949 18
2011949 0
2011949 6
2011949 12
2011949 18
(and so on)
Thus I want to add hours (00,06,12,18) to each date. The resulting file would have two columns (data and time). Please can you help me with this?
Thanks, Djordje

Akzeptierte Antwort

Geoff Hayes
Geoff Hayes am 1 Aug. 2014
Assuming that the file has been opened and all contents read into the matrix dateData (see dlmread for examples on reading numeric data from a text file) then you can do the following
% subset of the data
dateData = [1011949; 2011949; 3011949; 4011949; 5011949; 6011949; 7011949; 8011949];
% determine the number of rows/dates
numDates = size(dateData,1);
% for each date, reproduce it four times for the hours of 0, 6, 12, 18
dateData = cell2mat(arrayfun(@(x)repmat(x,4,1),dateData,'UniformOutput',false));
% now create a column of 0,6,12,18 repeated numDates times
hourData = [0; 6; 12; 18];
hourData = repmat(hourData,numDates,1);
% "attach" the hours to the dates
newData = [dateData hourData];
In re-constructing dateData we use arrayfun in order to apply a function to each element of the input array (so to each date). The function is repmat which allows each date to be replicated from a single element into a 4x1 array. The output from each date is a 4x1 array, each as an element in a single cell array. We then convert this cell array to a matrix using cell2mat.
We do something similar to hourData - we just replicate the 4x1 array numDates times for each of our dates. We then "add" the two columns together to create the final matrix.
Try the above and see what happens!

Weitere Antworten (2)

Sara
Sara am 1 Aug. 2014
fid1 = fopen('filewithdates','r');
fid2 = fopen('filewithhours','r');
fid3 = fopen('output.txt','w');
while 1
t1 = fget(fid1);t2 = fgetl(fid2);
if(~ischar(t1)),break,end
fprintf(fid3,'%s %s\n',t1,t2);
end
fclose all;

djr
djr am 1 Aug. 2014
Thank you all... you helped me a lot!

Kategorien

Mehr zu Dates and Time finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by