how to separate a large text file into individual text files based on date, but also export headers?
5 次查看(过去 30 天)
显示 更早的评论
Hi There,
I have a large text file with some data with the following headings, these run from 1991 to 2019, and i want to extract and generate individual text files , but keep the headings for later analysis. I also want the file name to be saved in a certain format based on the date.
I'm still pretty new to Matlab so any suggestions/ guidence would be great !
Easting Northing Elevation C hainage FC Profile_reg_ID Survey date
258574.81 309931.38 5.033 -65.24 ZZ 20 02/01/1992
Many thanks,
Alex
6 个评论
dpb
2023-1-27
", I need to seperate them by dates, so each individual date is stored as a seperate text file..."
That's easily-enough done, but I'd ask "Why?" create a zillion different files to have to process with all that extra overhead and code to deal with instead of just processing the data by whatever combination of variables needed? findgroups and groupsummary or rowfun are extremely powerful for such tasks...
采纳的回答
Star Strider
2023-1-27
编辑:Star Strider
2023-1-27
Try something like this —
files = {'https://www.mathworks.com/matlabcentral/answers/uploaded_files/1276100/Gwynedd_1991.txt';'https://www.mathworks.com/matlabcentral/answers/uploaded_files/1276165/Gwynedd_1992.txt'}
for k1 = 1:numel(files)
% fprintf('k1 = %d',k1)
T{k1} = readtable(files{k1}, 'VariableNamingRule','preserve');
if isnumeric(T{k1}.('Survey date'))
Date = datetime(T{k1}.('Survey date'), 'ConvertFrom','excel');
else
Date = T{k1}.('Survey date');
end
[G,Y,M,D] = findgroups(year(Date),month(Date),day(Date));
Gu = unique(G);
for k2 = 1:numel(Gu)
% fprintf('k2 = %d',k2)
SurvID = T{k1}.Profile_reg_ID(G==k2,:);
Dk = Date(G==k2);
[y,m,d] = ymd(Dk(1));
fn{k1,k2} = sprintf('%02d_%4d%02d%02d.txt',SurvID(1,1),y,m,d);
writetable(T{k1}(G==k2,:),fn{k1,k2})
fprintf('Written: %s\n',fn{k1,k2})
end
end
TR1 = readtable(fn{1,1}, 'VariableNamingRule','preserve')
TR6 = readtable(fn{2,6}, 'VariableNamingRule','preserve')
EDIT — (27 Jan 2023 at 16:05)
Adapted original code to accommodate both original files.
.
2 个评论
Star Strider
2023-1-27
As always, my pleasure!
I edited my original Answer to include and process both the original and added files.
.
更多回答(1 个)
Mathieu NOE
2023-1-27
So here we go
A code that is certainly not as refined as StarStrider's ....
filename = 'Gwynedd_1992.txt';
[outdata] = readcell(filename, 'DateTimeType', 'text');
[m,n] = size(outdata);
% extract header line and data
header_line = outdata(1,:);
data = outdata(2:m,:);
dates = string(data(:,n));
datesarray = datetime(dates);
% find unique dates
[dates_unic,ia,ic] = unique(datesarray);
% split and save individual data blocks (one file per date)
for ck = 1:numel(dates_unic)
start = ia(ck);
if ck == numel(dates_unic)
stop = m-1;
else
stop = ia(ck+1)-1;
end
thisdate = strrep(dates(start),'/','-');
data_out = [header_line; data(start:stop,:)];
% write to txt file
filename = strcat("surveyID_",thisdate,".txt");
writecell(data_out,filename,"Delimiter","tab");
end
2 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import and Export 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!