formatting text file using matlab
14 ビュー (過去 30 日間)
古いコメントを表示
Hi everyone, I have a text file with input data as below :
Here Is The Internal calendar ==> Year 2022/09/30 ::::WeekDay : 06
==> Time 15:21:06
****** Waiting for a Command to Start......******
Received a Command....
-17 -17 -11 -21 -13 -20 -11 -23 0000001
22 21 28 18 27 19 27 16 0000002
22 21 27 18 27 18 27 15 0000003
22 21 28 18 27 19 27 15 0000004
I want to re-format the text file with input above into a new text file with format as below
09/30/2022 03:33:16 PM -23 -11 -20 -13 -21 -11 -17 -17
09/30/2022 03:33:18 PM 16 27 19 27 18 28 21 22
09/30/2022 03:33:20 PM 15 27 18 27 18 27 21 22
09/30/2022 03:33:22 PM 15 27 19 27 18 28 21 22
Can anyone please show me how to do it ? thank you very much.
in case I have 20 text files like that, how can I read and re-format all 20 text file at 1 time code run ?
0 件のコメント
回答 (2 件)
Mathieu NOE
2022 年 10 月 19 日
編集済み: Mathieu NOE
2022 年 10 月 19 日
hello
let's start with one file ... then we will add the for loop to process many files in one operation
there is still one point which is not clear from me , is where to find the time increment between the four lines of data
we can see the time has incremented by 2 seconds between each successive line , but that increment is not something I could extract from the original data file. So for the time being it's hard coded.
the original data I saved it in a file : 'data001.txt'
fileDir = pwd; % choose your working directory
filename = 'data001.txt'; % filenames
out = do_the_job(fileDir, filename)
%%%%%%%%%%%% function %%%%%%%%%%%%%%%
function out = do_the_job(fileDir,filename)
D=readlines(fullfile(fileDir,filename)); % read as string array
nb_lines = numel(D);
% find date and put into format MM/DD/YY
ixP1=find(contains(D,'==> Year ')); % find the Year line
new_dat = strtrim(extractBetween(D(ixP1),'==> Year ','::::WeekDay'));
new_dat = split(new_dat,'/');
new_dat = [new_dat(2) new_dat(3) new_dat(1)];
new_dat = join(new_dat,'/');
% find time and put in new format
ixP2=find(contains(D,'==> Time')); % find the Time line
time = strtrim(extractAfter(D(ixP2),'==> Time'));
time = split(time,':');
hours = str2double(time(1));
if hours > 12
str = "PM ";
hours = hours -12;
else
str = "AM ";
end
% extract data array and reformat
ixP3=find(contains(D,'Received a Command')); % find the Received a Command line
data=str2double(split(strtrim(D(ixP3+1:nb_lines))));
data=data(:,1:end-1); % remove last column
data=data(:,end:-1:1); % flip columns (left right)
[md,nd] = size(data);
data_str = string(data);
% increment time lines by dt = 2 seconds per data line
for ck = 1:md
secs = str2double(time(3))+(ck-1)*2; % increment time lines by dt = 2 seconds per data line
secs_str = num2str(secs);
if secs<10
secs_str = strcat("0",secs_str);
end
time_out = [num2str(hours) time(2) secs_str];
time_out = join(time_out,':');
time_out = [time_out str];
time_out = join(time_out,' ');
% combine date and time strings
tmp = [new_dat time_out];
dat_and_time{ck,1} = join(tmp,' ');
end
% final assembly
out = join([dat_and_time data_str],' ');
% save to txt file
filename_out = ['out_' filename(1:length(filename)-4) '.txt'];
fid = fopen(fullfile(fileDir, filename_out), 'w' ); %// open file to writing
fprintf( fid, '%s\n', out ); %// print string to file
fclose( fid ); %// don't forget to close the file
disp(['File :' filename_out ' has been saved.']);
end
1 件のコメント
Mathieu NOE
2022 年 10 月 19 日
this is now the version that works for a complete folder with multiple files
hope it helps
NB : make sure you have installed that FEX submission (natsortfiles) that allows to sort files better than what matlab does by default
fileDir = pwd; % choose your working directory
S = dir(fullfile(fileDir,'data0*.txt')); % get list of data files in directory
S = natsortfiles(S); % sort file names into natural order , see :
%(https://fr.mathworks.com/matlabcentral/fileexchange/47434-natural-order-filename-sort)
for k = 1:length(S)
out = do_the_job(fileDir, S(k).name)
end
%%%%%%%%%%%% function %%%%%%%%%%%%%%%
function out = do_the_job(fileDir,filename)
D=readlines(fullfile(fileDir,filename)); % read as string array
nb_lines = numel(D);
% find date and put into format MM/DD/YY
ixP1=find(contains(D,'==> Year ')); % find the Year line
new_dat = strtrim(extractBetween(D(ixP1),'==> Year ','::::WeekDay'));
new_dat = split(new_dat,'/');
new_dat = [new_dat(2) new_dat(3) new_dat(1)];
new_dat = join(new_dat,'/');
% find time and put in new format
ixP2=find(contains(D,'==> Time')); % find the Time line
time = strtrim(extractAfter(D(ixP2),'==> Time'));
time = split(time,':');
hours = str2double(time(1));
if hours > 12
str = "PM ";
hours = hours -12;
else
str = "AM ";
end
% extract data array and reformat
ixP3=find(contains(D,'Received a Command')); % find the Received a Command line
data=str2double(split(strtrim(D(ixP3+1:nb_lines))));
data=data(:,1:end-1); % remove last column
data=data(:,end:-1:1); % flip columns (left right)
[md,nd] = size(data);
data_str = string(data);
% increment time lines by dt = 2 seconds per data line
for ck = 1:md
secs = str2double(time(3))+(ck-1)*2; % increment time lines by dt = 2 seconds per data line
secs_str = num2str(secs);
if secs<10
secs_str = strcat("0",secs_str);
end
time_out = [num2str(hours) time(2) secs_str];
time_out = join(time_out,':');
time_out = [time_out str];
time_out = join(time_out,' ');
% combine date and time strings
tmp = [new_dat time_out];
dat_and_time{ck,1} = join(tmp,' ');
end
% final assembly
out = join([dat_and_time data_str],' ');
% save to txt file
filename_out = ['out_' filename(1:length(filename)-4) '.txt'];
fid = fopen(fullfile(fileDir, filename_out), 'w' ); %// open file to writing
fprintf( fid, '%s\n', out ); %// print string to file
fclose( fid ); %// don't forget to close the file
disp(['File :' filename_out ' has been saved.']);
end
Dung Tran
2022 年 10 月 19 日
2 件のコメント
Mathieu NOE
2022 年 10 月 21 日
hello Dung
ok I see the issue
to make sure I solved it can you share one (or several ) original files ?
tx
参考
カテゴリ
Help Center および File Exchange で Dates and Time についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!