MATLAB Excel date column export into Matlab

AI Thread Summary
The discussion focuses on exporting a date column from an Excel file into MATLAB for plotting. The user encountered an error because MATLAB only recognized one column of data, mistaking the date column for the second column. A workaround was suggested, involving adding an empty column in the spreadsheet. The user successfully used the `datenum` function to convert the date format for plotting. Additionally, they inquired about using `datetick` to format the x-axis labels in 'dd-mmm-yyyy' format, with guidance provided on using the `datestr` function for date formatting.
adeeyo
Messages
20
Reaction score
0
Hi everybody,
I need assistance. I have excel file that conntains a date column and another column. I wish to plot the date on x-axis and the second column on y axis. I wrote the MATLAB code below.

This is my code. data=xlsread('DataTest','Sheet1'); q=data(:,2); time=data(:,1);

Error message

Attempted to access data(:,2); index out of bounds because size(data)=[193,1].

Matlab is not reading the first column of the excel file that contains the date so it takes the second column as the first column as a result it could not find second column. Please help
 
Physics news on Phys.org
http://www.mathworks.com/help/matlab/import_export/when-to-convert-dates-from-excel-files.html
 
As a workaround, what happens if you add an empty column in the spreadsheet?
 
Thanks Kreil,
I have succeeded in using datenum. See the code below
wt=xlsread('DataTest','sheet1');
datecol = 1; wt(:,datecol) = wt(:,datecol) + datenum('30-Dec-1899'); x=wt(:,1); qo=wt(:,2); plot(x,qo);

How do I use datetick to get the x (date axis) in day-month-year ('dd-mmm-yyyy') format?
Thanks Isa
 
datetick is a function for adding the date to tick labels on a plot.

Use the datestr() function to reverse what datenum() does. For example,

Code:
>> d = datenum('30-Dec-1899')

d =

      693960

>> datestr(d)

ans =

30-Dec-1899
 

Similar threads

Replies
8
Views
2K
Replies
2
Views
4K
Replies
11
Views
4K
Replies
18
Views
6K
Replies
2
Views
2K
Replies
3
Views
3K
Replies
3
Views
28K
Back
Top