0

I would like to plot a time series in Matlab of a data set I have in Excel.

The Excel file looks as follows:

Data:       |  Value:

2005-04-01  |  5.20

2006-12-02  |  3.12

...

How could I load this into Matlab and plot the time series of it?

5
  • Hint: have a look at xlsread and plot.
    – am304
    Apr 22, 2015 at 14:10
  • @am304 Yes... but of course I did so already :-) I then get the dates as string, which I don't want...
    – p_thomson
    Apr 22, 2015 at 14:34
  • Then you should tell us in your question what you've tried, with your code, and where you got stuck, with any error message. Right now, your question is too vague and suggests you haven't tried anything.
    – am304
    Apr 22, 2015 at 15:28
  • I think the question is quite clear, despite not having tried anything yet. Apr 22, 2015 at 16:44
  • @p_thomson Can you verify if my solution is what you were looking for? Apr 23, 2015 at 1:08

2 Answers 2

1

There's 2 easy way of plotting dates, but I'll give you the script to read from the xls file first.

% Read from Excel
[N,T] = xlsread( filepath );

You then need to extract/convert the dates as follows. Dates are the 1st column of the text.

d = datetime( T(:,1) );

Then you can plot the variables as follows

figure;
plot( d, N(:,1) );

A sample plot is here

Date Plot

Alternatively, you can use datenum instead of datetime if you want the date as an integer instead of a datetime object using the following line.

d = datenum( T(:,1) );
0

use xlsread to load in data as string, now converting the date and values can be done in a number of ways, the most strait forward way for your dates is perhaps using the str2num which allows you to read in the N th character as a number, for example:

string = "2005-04-01|5.20"
year = str2num(string(1:4))
month = str2num(string(6:7)) //%where the number is the N th character in string, and has to be numerical or the str2num will return error

//%Note that this method does not read non-numerical strings, in your case "-" "|" will not be read. 

I believe this is one of the faster methods, since it mainly involve a type conversion on a standard format of data. Any other suggestions?

PS: in your time series plot, I would suggest converting your dates into a long number, i.e. 20050401, 20061202, etc; which would be most efficient. (you can do this by years*1000+months*100+days.

3
  • While this solution will help parse/extract the year and month, I don't think this is what the OP wanted Apr 22, 2015 at 16:45
  • Also the long number isn't a linear scaling, which is what you usually want Apr 22, 2015 at 16:46
  • My datenum method would work better, but that's already included in my solution Apr 22, 2015 at 16:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.