Convert dates from Excel to Matlab
Show older comments
I am fairly new to Matlab and therefore have some problems related of importing data.
Hey all,
I want to import GDP data and then plot it to time. I was able to import it and translate to string dates. Now I would like to convert it back to my target Matrix. However, I just see values 48 or 49 in my target matrix.
My code is as follows:
AUSGDP = zeros(228,2);
GDP = xlsread('AUDGDP.xlsx');
GDP = GDP(1:228,1) + 693960;
datestring = datestr(GDP,'mm/yyyy');
AUSGDP(228,1) = datestring(228,1)
Thx in advance,
Chris
Answers (2)
Walter Roberson
on 10 Sep 2016
AUSGDP = zeros(228,2);
declares that AUSGDP is to be an array of double precision numbers.
datestring = datestr(GDP,'mm/yyyy');
converts the serial date numbers stored in GDP into strings; in particular into an array of characters.
AUSGDP(228,1) = datestring(228,1)
extracts one of those characters and stores it into one of the floating point numbers. The characters in the first column of datestr('mm') are going to be two digit month numbers, so the first of those characters is going to be either '0' (for months up to September) or '1' (for October to December.) The character '0' has numeric value 48 and the character '1' has numeric value 49; that is char(48) == '0' is true. And 48 and 49 are what are showing up in your output, so your code is doing what you asked it to do.
Peter Perkins
on 13 Sep 2016
0 votes
If you're using R2014b or newer, suggest you look into using datetime, which has explicit conversions to and from excel serial dates. Ideally, you won't need to convert back, because datetimes are much easier to use that the output of datestr, including in plotting.
Hope this helps.
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!