Combining large text files of different sizes

1 view (last 30 days)
Todd Gilbert
Todd Gilbert on 30 Jan 2015
Commented: dpb on 18 Feb 2015
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
Todd Gilbert
Todd Gilbert on 17 Feb 2015
Will you please add some granularity/detail to your explanation?
dpb
dpb on 18 Feb 2015
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

Find more on Cell Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!