Combining large text files of different sizes

I have two large text files containing data that I want to load into MATLAB and place into a single matrix (:,3)
  • The files are different sizes, but each contain time values in the first column.
  • The files contain some superfluous header information on the first few rows, that I will need to comb out
  • The time data is formatted differently in each file, so I will need to reformat using one scheme.
  • I want to merge them, so I end up with three columns: time, data1 and data2; with the time elements blended to create one sorted time column with appropriately aligned/coordinated data
What is the most-efficient method?

3 Comments

Read each file into memory per it's formatting, do the conversions and merge. For this, clearly only need each independently of the other to create the single merged file. Then, you can read the final and do the sort. If not so large but what you can hold both, then can save the one read/write step.
Will you please add some granularity/detail to your explanation?
What's to say?
Open and read the two files independently...
fid=fopen('file1');
d1=textscan(fid,fmt1,'headerlines',N,'collectoutput',1);
fid=fclose(fid);
fid=fopen('file2');
d2=textscan(fid,fmt2,'headerlines',M,'collectoutput',1);
fid=fclose(fid);
Convert the time columns to datenums and sort; keep the order index for the other data to go with revised order...
[dn,idx=sort[datenum(d1{1},'properformat');
datenum(d2{1},'properformat')]);
Convert the other cell to array; concatenate and rearrange into single array
d=[cell2mat(d1{2});dn2 cell2mat(d2{2})];
d=[dn d(idx)];
You'll have to fixup the formatting differences, number of headerlines, etc., etc., etc., of which you gave no details...

Sign in to comment.

Answers (0)

Categories

Asked:

on 30 Jan 2015

Commented:

dpb
on 18 Feb 2015

Community Treasure Hunt

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

Start Hunting!