date and time string identification

2 vues (au cours des 30 derniers jours)
Jorge Luis Paredes Estacio
Jorge Luis Paredes Estacio le 19 Déc 2022
Modifié(e) : Stephen23 le 19 Déc 2022
Hello, I have the following information obtained from a text-file
date_hne: {'2014' '04' '01' '23' '46' '44.998300'}
date_hnn: {'2014' '04' '01' '23' '46' '44.998300'}
date_hnz: {'2014' '04' '01' '23' '46' '44.998300'}
as output in a cell array. I need to convert this information into a date vector that identify the date and time using the following command
formatIn = 'yyyy-MM-dd HH:mm:ss.SSS'
date_hne = datestr(date_hne,formatIn)
date_hnn = datestr(date_hnn,formatIn)
date_hnz = datestr(date_hnz,formatIn)
The seconds can be rounded as 45
The readable file is an compressed file (wwith 3 files) in order to be read. I can not attached the file that is why I attached as an uncompressed file here.
The script is as follows
%% Sub-function 5: this works for the data from the CENTRO SISMOLOGICO NACIONAL (CSN) OF CHILE
function [date_hne, date_hnn, date_hnz] = import_zip_chile (filename)
if strcmp(filename(end-2:end),'zip')==1
error('Not a Zip');
cd ..
fname = content(1).name;
fname = fname(1:end-8);
formatIn = 'yyyy-MM-dd HH:mm:ss.SSS'
% S = dir(fullfile(path,[file_name,'*.txt']));
index_station = 0; %index added for stations
%% create list of unique filenames like 20211104-041752-C16O
% list of unique files :
% load "HNE"
file_name_1 = [fname, '-HNE.txt'];
file_name_1 = strrep(file_name_1,'_','-');
[header_hne] = import_from_CSN_chile(['tmp\',file_name_1],1);
file_name_1 = [fname, '-HLE.txt'];
file_name_1 = strrep(file_name_1,'_','-');
[header_hne] = import_from_CSN_chile(['tmp\',file_name_1],1);
number_of_rows_header = min(size(header_hne));
for i=1:number_of_rows_header
if strcmp(header_hne(i,1:length('# Tiempo de Origen:')),'# Tiempo de Origen:')==1
line_date = i;
date_hne = regexp(header_hne(line_date,:),'-?\d+\.?\d*|-?\d*\.?\d+','match'); % date and time
%date_hne = join(date_hne);
date_hne = date_hne(~isspace(date_hne));
date_hne = erase(date_hne,"-"); %erase dash fromt the string
date_hne = datestr(date_hne); %added to convert to date string
% load "HNN"
file_name_2 = [fname, '-HNN.txt'];
file_name_2 = strrep(file_name_2,'_','-');
[header_hnn] = import_from_CSN_chile(['tmp\',file_name_2],1);
file_name_2 = [fname, '-HLN.txt'];
file_name_2 = strrep(file_name_2,'_','-');
[header_hnn] = import_from_CSN_chile(['tmp\',file_name_2],1);
date_hnn = regexp(header_hnn(line_date,:),'-?\d+\.?\d*|-?\d*\.?\d+','match'); % date and time
%date_hnn = join(date_hnn);
date_hnn = date_hnn(~isspace(date_hnn));
date_hnn = erase(date_hnn,"-"); %erase dash fromt the string
date_hnn = datestr(date_hnn); %added to convert to date string
% load "HNZ"
file_name_3 = [fname, '-HNZ.txt'];
file_name_3 = strrep(file_name_3,'_','-');
[header_hnz] = import_from_CSN_chile(['tmp\',file_name_3],1);
file_name_3 = [fname, '-HLZ.txt'];
file_name_3 = strrep(file_name_3,'_','-');
[header_hnz] = import_from_CSN_chile(['tmp\',file_name_3],1);
date_hnz = regexp(header_hnz(line_date,:),'-?\d+\.?\d*|-?\d*\.?\d+','match'); % date and time
%date_hnz = join(date_hnz);
date_hnz = date_hnz(~isspace(date_hnz));
date_hnz = erase(date_hnz,"-"); %erase dash fromt the string
date_hnz = datestr(date_hnz); %added to convert to date string
rmdir ('tmp','s')
%%function to import from chile
function [head] = import_from_CSN_chile(filename,nclm,skip,formt)
% READCLM Reads numerical data from a text file into a matrix.
% Text file can begin with a header or comment block.
% Opens file FILENAME, skips first several lines specified
% by SKIP number or beginning with comment '%'.
% Then reads next several lines into a string matrix HEAD
% until the first line with numerical data is encountered
% (that is until first non-empty output of SSCANF).
% Then reads the rest of the file into a numerical matrix
% DATA in a format FORMAT with number of columns equal
% to number of columns of the text file or specified by
% number NCLM. If data does not match the size of the
% matrix DATA, it is padded with NaN at the end.
% READCLM(FILENAME) reads data from a text file FILENAME,
% skipping only commented lines. It determines number of
% columns by the length of the first data line and uses
% the floating point format '%g';
% READCLM uses FGETS to read the first lines and FSCANF
% for reading data.
% Defaults and parameters ..............................
formt_dflt = '%g'; % Default format for fscanf
addn = nan; % Number to fill the end if necessary
% Handle input ..........................................
if nargin<1, error(' File name is undefined'); end
if nargin<4, formt = formt_dflt; end
if nargin<3, skip = 0; end
if nargin<2, nclm = 0; end
if isempty(nclm), nclm = 0; end
if isempty(skip), skip = 0; end
% Open file ............................
[fid,msg] = fopen(filename);
if fid<0, disp(msg), return, end
% Find header and first data line ......................
is_head = 1;
jl = 0;
head = ' ';
while is_head % Add lines to header.....
s = fgets(fid); % Get next line
jl = jl+1;
is_skip = jl<=skip;
is_skip = jl<=skip | s(1)=='%';
out1 = sscanf(s,formt); % Try to read this line
% If unreadable by SSCANF or skip, add to header
is_head = isempty(out1) | is_skip;
if is_head & ~is_skip
head = str2mat(head,s(1:length(s)-1)); end
head = head(2:size(head,1),:);
% Determine number of columns if not specified
out1 = out1(:)';
l1 = length(out1);
if ~nclm, nclm = l1; end
fclose (fid); % Close file ..........
  1 commentaire
Stephen23 le 19 Déc 2022
Modifié(e) : Stephen23 le 19 Déc 2022
It is unclear what your actual question is.
This is very smelly code:
cd ..
Do not use CD just to access data files or call DIR. Using CD is slow and makes debugging harder. You should use absolute/relative filepaths.
Deleting elements 1 & 2 from the structure returned by DIR() is a buggy way to handle the dot directory names:
As Walter Roberson wrote in that last link: "In short: if your code assumes that '.' and '..' are the first two entries in a directory, your code has a bug (even in MS Windows). If your code assumes that directory entries are returned in any sorted order, your code has a bug (in all OS.)"
The robust approach is one of these:
  • use ISMEMBER, SETDIFF, or similar on the names.
  • specify the DIR() match string to exclude dot directory names.

Connectez-vous pour commenter.

Réponses (1)

Peter Perkins
Peter Perkins le 19 Déc 2022
Modifié(e) : Peter Perkins le 19 Déc 2022
"I have the following information obtained from a text-file
date_hne: {'2014' '04' '01' '23' '46' '44.998300'}"
I don't see anything even remotely like that in the three text files you've attached. And that's not a great format to end up with. post more information on how you got there, and people can probably help.
Also: these two things
formatIn = 'yyyy-MM-dd HH:mm:ss.SSS'
date_hne = datestr(date_hne,formatIn)
are incompatible. The first is a datetime format. The second is a call to the legacy datestr function, which uses a very different set of formats. Don't use datestr.


En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by