0% found this document useful (0 votes)
38 views6 pages

Treinamento em MATLAB - Manipulando Datas e Horas Lázaro

The document discusses various MATLAB commands for manipulating dates and times: 1) The datestr command converts a serial date number to a string representation of a date. Datetimes in MATLAB can be represented as strings, vectors, or serial numbers. 2) The datenum command converts a date string to a serial date number. 3) The datevec command converts a serial date number to a vector representation of the date with fields for year, month, day, etc. 4) The datetick command formats the tick marks and labels on a plot's x-axis to display dates when the x-data is represented as serial date numbers.

Uploaded by

Jean Pierre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views6 pages

Treinamento em MATLAB - Manipulando Datas e Horas Lázaro

The document discusses various MATLAB commands for manipulating dates and times: 1) The datestr command converts a serial date number to a string representation of a date. Datetimes in MATLAB can be represented as strings, vectors, or serial numbers. 2) The datenum command converts a date string to a serial date number. 3) The datevec command converts a serial date number to a vector representation of the date with fields for year, month, day, etc. 4) The datetick command formats the tick marks and labels on a plot's x-axis to display dates when the x-data is represented as serial date numbers.

Uploaded by

Jean Pierre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Treinamento em MATLAB - Manipulando datas e horas Lázaro

Comando datestr

Converte um número em data (cadeia de caracteres). A data base 1 é 01 de janeiro do ano 0000 (01-01-0000).

Datas e horários são expressados no MATLAB de três formas:

Date String: '24-Oct-2003 12:45:07'


Date Vector: [2003 10 24 12 45 07]
Serial Date Number: 7.3188e+005

1
1) Digite os seguintes comando no MATLAB:

>> datestr(now)

ans =

06-Jan-2010 10:25:52

> dt = datestr(now, 'mmm dd, yyyy HH:MM:SS.FFF AM')

dt =

Jan 06, 2010 11:07:41.920 AM

>> datestr(now, 2)

ans =

01/06/10

>> datestr(now, 'mm/dd/yy')

ans =

01/06/10

>> datestr(now, 'dd.mm.yyyy')

ans =

06.01.2010

2
>> datestr(datenum('09.01.2010','dd.mm.yyyy'), 2)

ans =

01/09/10

>> datestr(datenum('09.01.2010','dd.mm.yyyy'), 8)

ans =

Sat

Comando datenum

Converte data (cadeia de caracteres) em número. A data base é 01 de janeiro do ano 0000 (01-01-0000).

2) Digite os seguintes comando no MATLAB:

>> n = datenum('09-Jul-2009','dd-mmm-yyyy')

n =

733963

>> n = datenum('01-Jan-0000','dd-mmm-yyyy')

n =

1
>> n = datenum(2010, 01, 09)

n =

734147

>> format bank


>> datenum('March 28, 2005 3:37:07.033 PM')

ans =

732399.65

>> n = datenum('12-jun-17','dd-mmm-yy')

n =

736858.00

>> n = datenum('12-jun-17','dd-mmm-yy',1400)

n =

517712.00

Comando datevec

Converte um número em data (cadeia de caracteres).

3) Digite os seguintes comando no MATLAB:

>> format short g


>> datevec('January 9, 2010 9:50:08.980 AM')

ans =
3
2010 1 9 9 50 8.98

>> t = datenum('January 9, 2010 9:50:08.980 AM')

t =

7.3415e+005

>> datevec(t)

ans =

2010 1 9 9 50 8.98

>> [y, m, d, h, mn, s] = datevec('January 9, 2010 9:50:08.980 AM')

>> sprintf('Data: %d/%d/%d Hora: %d:%d:%2.3f\n', m, d, y, h, mn, s)

>> datevec('25.04.2003', 'dd.mm.yyyy')

ans =

2003 4 25 0 0 0

Comando datetick

Formata os eixos de um grafico no modo data.

datetick(tickaxis,dateformat)

datetick(tickaxis,dateformnum)

4) Digite os seguintes comando no MATLAB:

>> t = (1900:10:1990)';
>> p = [60.454 90.456 102.345 123.467 130.433 148.433 167.445 202.221 226.709 250.443]

>> plot(datenum(t,1,1),p)
>> grid on
>> datetick('x',11)

5) Modifique o exemplo acima, para mostrar na plotagem, os anos no formato ‘yyyy’;

4
6) Digite os seguintes comando no MATLAB:

>> data_inicio = datenum('01-01-2010')

>> data_final = datenum('12-31-2010')

>> xData = linspace(data_inicio,data_final,12);

>> plot(xData,rand(1,12))
>> set(gca,'XTick',xData)
>> datetick('x','mmm','keepticks')

7) Crie o arquivo “trafego.m” e execute:

%script trafego.m

load count.dat %carrega os dados do arquivo count.dat

n = length(count);

ano = 1990 * ones(1,n);


mes = 4 * ones(1,n);
dia = 18 * ones(1,n);

hora = 1:n;

minutos = zeros(1,n);

xdate = datenum(ano,mes,dia,hora,minutos,minutos);

plot(xdate,count)

datetick('x','HH:MM PM')

Comando calendar

Gera uma matriz com o calendário da datta solicitada.

c = calendar(ano,mes)

8) Digite os seguintes comando no MATLAB:

>> c = calendar(2010,1)

c =

0 0 0 0 0 1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31 0 0 0 0 0 0

5
Comando addtodate

Adiciona um valor a uma data.

R = addtodate(D, N, tipo)

D : data em numero real


N : quantidade a ser adicionada
Tipo: ‘day’, ‘month’, ‘year’

9) Digite os seguintes comando no MATLAB:

>> t = datenum('07-Apr-2008 23:00:00');


>> datestr(t)

ans =

07-Apr-2008 23:00:00

>> t = addtodate(t,2,'day');
>> datestr(t)

ans =

09-Apr-2008 23:00:00

>> t = addtodate(t,-7,'day');
>> datestr(t)

ans =

02-Apr-2008 23:00:00

>> R = addtodate(datenum('12/24/2007 12:45'), 20, 'day');

>> datestr(R)

ans =

13-Jan-2008 12:45:00

10) Crie o arquivo “data_5.m”, execute e explique seu funcionamento:

% arquivo data_5.m

s1 = datestr(now,13)
pause(5)
s2 = datestr(now,13)

datestr(datenum(s1)+datenum(s2),13)

You might also like