How to get matlab to write txt file without a column from the big text file its split from

4 vues (au cours des 30 derniers jours)
Hi there,
I'm processing a large text file with beach profiles to put into another program by splitting it into dates, the original txt file has dates in it, which are used to split the file in the needed organisation. So the columns in the original file are :
Easting Northing Elevation Chainage FC Profile Reg_ID Survey date
However the programme I am using strictly needs to just contain:
Easting Northing Elevation Chainage FC Profile Reg_ID
I have got some code working that splits and exports the large file into text files based on the date, but is there a way of making sure the date column isnt written in those text files? At the moment it is outputting the seperated files into :
Easting Northing Elevation Chainage FC Profile Reg_ID Survey date
I need the survey date removed completely for it to run in the next programm, but I'm a bit stuck on how to do this.
If there are any suggestions, I would be most greatful as I'd like to avoid manually changing all of the files if possible?
%####################################################################################
% WHAT DOES IT DO?
% This code imports and splits a big txt file containing beach profiles into multiple files based on
% the date to create seperate txt files for use in another programme.
% It outputs a number of text files that are seperated based on the date,
% so for example all profiles taken on 01/01/2001 will be exported to a
% seperate txt file.
%####################################################################################
clear all
close all
clc
%####################################################################################
files = {'Example_large.txt'}
files = 1×1 cell array
{'Example_large.txt'}
% Starting the forloop.
for k1 = 1:numel(files)
% fprintf('k1 = %d',k1)
%T{k1}creates the cell array with the k1 element in the cell structure
% ans then reads in the element {k1}. It is saying the contents of {k1} read
% table files{k1} (files containing k1) and then telling it to preserve the
% headings in the txt file.
% This part is telling Matlab to read the files with elements
% from K1, then if the date is numeric convert it to matlab format if
% stored on excel number version.
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
%this part of the code finds and seperates all the seperate values by
% year moth day - this then gives a unique number of values based on
% the number of groups that have unique dates as 'g'.
[G,Y,M,D] = findgroups(year(Date),month(Date),day(Date));
Gu = unique(G);
% This nested for loop indexed by k2 this will run from 1 to the number of
% unique values derived from in the previous loop. DK is where they are
% seperated based on the profiles with the same date.
for k2 = 1:numel(Gu)
% fprintf('k2 = %d',k2)
SurvID = T{k1}.Reg_ID(G==k2,:);
Dk = Date(G==k2);
[y,m,d] = ymd(Dk(1));
% defining the file name
fn{k1,k2} = sprintf('%02d_%4d%02d%02d.txt',SurvID(1,1),y,m,d);
% writing the table
writetable(T{k1}(G==k2,:),fn{k1,k2},'writevariablenames', true,Delimiter="tab");
%T{k1}.Properties.Profile_reg_ID = 'Profile Reg_ID';
%T{k1} = renamevars(T{k1},"Profile_reg_ID","Profile Reg_ID")
fprintf('Written: %s\n',fn{k1,k2})
end
end
Written: 11_19920203.txt Written: 10_19920204.txt Written: 01_19920205.txt Written: 01_19920908.txt Written: 01_19930514.txt Written: 10_19930518.txt Written: 01_19931022.txt Written: 10_19931025.txt Written: 01_19940511.txt Written: 01_19940930.txt Written: 10_19941004.txt Written: 01_19950424.txt Written: 01_19951101.txt Written: 01_19960415.txt Written: 01_19960923.txt Written: 01_19970402.txt Written: 01_19971013.txt Written: 01_19980403.txt Written: 10_19980406.txt Written: 10_19981015.txt Written: 01_19981020.txt Written: 01_19990412.txt Written: 01_19991004.txt Written: 10_19991005.txt Written: 01_20000503.txt Written: 01_20001010.txt Written: 10_20001011.txt Written: 01_20010516.txt Written: 10_20010517.txt Written: 01_20011113.txt Written: 01_20020422.txt Written: 10_20020425.txt Written: 01_20021106.txt Written: 10_20021107.txt Written: 01_20030424.txt Written: 10_20030528.txt Written: 10_20031013.txt Written: 01_20031123.txt Written: 01_20040406.txt Written: 10_20040520.txt Written: 10_20040928.txt Written: 01_20041013.txt Written: 10_20050408.txt Written: 01_20050410.txt Written: 10_20050917.txt Written: 01_20051015.txt Written: 10_20060524.txt Written: 10_20060925.txt Written: 10_20071011.txt Written: 10_20080419.txt Written: 10_20080915.txt Written: 10_20090408.txt Written: 10_20090916.txt Written: 10_20100428.txt Written: 10_20101007.txt Written: 10_20110418.txt Written: 01_20110420.txt Written: 10_20111012.txt Written: 10_20120423.txt Written: 10_20121015.txt Written: 10_20130425.txt Written: 10_20131017.txt Written: 10_20140415.txt Written: 10_20141007.txt Written: 10_20150421.txt Written: 10_20151013.txt Written: 10_20160509.txt Written: 10_20161017.txt Written: 10_20170410.txt Written: 01_20170420.txt Written: 10_20171019.txt Written: 10_20180416.txt Written: 10_20180925.txt

Réponse acceptée

Mathieu NOE
Mathieu NOE le 28 Mar 2023
hello
this is a minor change to your code
look for the two lines where I added the comment : % <= here
basically I created a second temporary table without the last column and this table is what is used to create the splitted txt files at the end
T_no_date = T{k1}(:,1:end-1); % remove last column (Survey date); % <= here
.................................................
% writing the table
writetable(T_no_date(G==k2,:),fn{k1,k2},'writevariablenames', true,"Delimiter","tab"); % <= here
%####################################################################################
% WHAT DOES IT DO?
% This code imports and splits a big txt file containing beach profiles into multiple files based on
% the date to create seperate txt files for use in another programme.
% It outputs a number of text files that are seperated based on the date,
% so for example all profiles taken on 01/01/2001 will be exported to a
% seperate txt file.
%####################################################################################
clear all
close all
clc
%####################################################################################
files = {'Example_large.txt'}
% Starting the forloop.
for k1 = 1:numel(files)
% fprintf('k1 = %d',k1)
%T{k1}creates the cell array with the k1 element in the cell structure
% ans then reads in the element {k1}. It is saying the contents of {k1} read
% table files{k1} (files containing k1) and then telling it to preserve the
% headings in the txt file.
% This part is telling Matlab to read the files with elements
% from K1, then if the date is numeric convert it to matlab format if
% stored on excel number version.
T{k1} = readtable(files{k1}, 'VariableNamingRule','preserve');
T_no_date = T{k1}(:,1:end-1); % remove last column (Survey date); % <= here
if isnumeric(T{k1}.('Survey date'))
Date = datetime(T{k1}.('Survey date'), 'ConvertFrom','excel');
else
Date = T{k1}.('Survey date');
end
%this part of the code finds and seperates all the seperate values by
% year moth day - this then gives a unique number of values based on
% the number of groups that have unique dates as 'g'.
[G,Y,M,D] = findgroups(year(Date),month(Date),day(Date));
Gu = unique(G);
% This nested for loop indexed by k2 this will run from 1 to the number of
% unique values derived from in the previous loop. DK is where they are
% seperated based on the profiles with the same date.
for k2 = 1:numel(Gu)
% fprintf('k2 = %d',k2)
SurvID = T{k1}.Reg_ID(G==k2,:);
Dk = Date(G==k2);
[y,m,d] = ymd(Dk(1));
% defining the file name
fn{k1,k2} = sprintf('%02d_%4d%02d%02d.txt',SurvID(1,1),y,m,d);
% writing the table
writetable(T_no_date(G==k2,:),fn{k1,k2},'writevariablenames', true,"Delimiter","tab"); % <= here
%T{k1}.Properties.Profile_reg_ID = 'Profile Reg_ID';
%T{k1} = renamevars(T{k1},"Profile_reg_ID","Profile Reg_ID")
fprintf('Written: %s\n',fn{k1,k2})
end
end

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by