How to change the date format of ,txt file?

Hi, I am having a hard time on this. I need to read a sequence of .txt files (wind speed and direction forecasts).
I am storing the files names in an array called modfile:
t1 = datetime(2019,7,2);
a=dir(('C:\Users\jmarc\PGE\Dados\Previstos\*.txt'));
numfiles=size(a,1);
mydata = cell(1, numfiles);
for k = 1:numfiles
dia=sprintf( '%02d', day(t1));
mes=sprintf( '%02d', month(t1));
ano=sprintf( '%04d', year(t1));
amd=[ano,'-',mes,'-',dia];
myfiletxt=['C:\Users\jmarc\PGE\Dados\Previstos\','Previsao_Total_',amd,'.txt'];
modfile(k)=string(myfiletxt);
t1=t1+1;
end
for i=1:length(modfile)
modi = readtable(modfile(i));
...
The timestamp of the first column of each file is like this:
2019-07-02 0:30:00
It turns out when matlab reads the timestamp (modi file), day and month are reversed.
Instead of 02-Jul-2019, it gives 07-Feb-2019.
I tried many different formats when reading modfile(i), but I was not successful.
Does anyone can help me?

6 commentaires

Ilian
Ilian le 7 Avr 2020
Have you tried to use the textscan function to read the file? There you can specify the date format. See here for an example:
https://www.mathworks.com/help/matlab/ref/textscan.html
JOAO LIMA
JOAO LIMA le 7 Avr 2020
Thanks, Ilian! I will try it.
dpb
dpb le 7 Avr 2020
Modifié(e) : dpb le 7 Avr 2020
Use the detectImportOptions function to create a DelimitedTextImportOptions object for the file. In it you can set the 'DateFormat' for the date/time variable(s) with the setvaropts function.
Then pass the object when call readtable.
Same effect as w/ textscan but get the table directly as well as avoid fopen-fclose hassle and the need to then convert the raw data to the table.
JOAO LIMA
JOAO LIMA le 8 Avr 2020
Thanks for your support! Actually I realize that problem starts when I write the txt files from a excel files.
I am using xlsread to load the excel files, extracting sheet #4, and write it in txt format.
I am working on it, but not yet fixed.
dpb
dpb le 8 Avr 2020
xlsread has been deprecated--use one of the suggested alternatives at doc xlsread depending on the format of the data in the sheet..
JOAO LIMA
JOAO LIMA le 8 Avr 2020
Modifié(e) : JOAO LIMA le 8 Avr 2020
dpb, I got it, following your first answer. See below.

Connectez-vous pour commenter.

 Réponse acceptée

Jeremy Hughes
Jeremy Hughes le 7 Avr 2020

0 votes

2 commentaires

JOAO LIMA
JOAO LIMA le 8 Avr 2020
Thank you!! I really worked! Just one thing: for me worked 'DatetimeFormat' instead of 'DateTimeFormat'
opts = detectImportOptions(modfile(i));
opts = opts.setvaropts('data11','InputFormat','dd/MM/yyyy HH:mm:ss','DatetimeFormat','dd-MMM-yyyy HH:ss');
modi = readtable(modfile(i),opts);
BTW, if the format of the files are all the same, you can re-use the opts
opts = detectImportOptions(modfile(1));
opts = opts.setvaropts('data11','InputFormat','dd/MM/yyyy HH:mm:ss','DatetimeFormat','dd-MMM-yyyy HH:ss');
for i = 1:numel(modfile)
modi = readtable(modfile(i),opts);
end

Connectez-vous pour commenter.

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