Excel date column export into Matlab

  • Context: MATLAB 
  • Thread starter Thread starter adeeyo
  • Start date Start date
  • Tags Tags
    Column Excel Matlab
Click For Summary

Discussion Overview

The discussion revolves around exporting a date column from an Excel file into MATLAB for plotting purposes. Participants address issues related to reading the date data correctly and formatting it for visualization.

Discussion Character

  • Technical explanation, Homework-related, Exploratory

Main Points Raised

  • One participant reports an error in MATLAB when attempting to access the second column of data, indicating that MATLAB is not reading the date column correctly from the Excel file.
  • Another participant shares a link to MATLAB documentation regarding the conversion of dates from Excel files.
  • A workaround is suggested involving the addition of an empty column in the spreadsheet to potentially resolve the reading issue.
  • A later reply indicates success in using the datenum function to convert the date column, followed by a request for assistance on using datetick to format the x-axis labels in a specific date format.
  • Another participant explains the purpose of the datetick function and provides an example of using the datestr function to convert a date number back to a string format.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best method to resolve the initial issue with reading the date column, as multiple approaches are discussed without agreement on a single solution.

Contextual Notes

There are limitations regarding the assumptions about how MATLAB reads Excel files, and the discussion does not resolve the underlying issue of the data structure in the Excel file.

Who May Find This Useful

Individuals working with MATLAB and Excel data exports, particularly those dealing with date formatting and plotting issues.

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 ·
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 11 ·
Replies
11
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 18 ·
Replies
18
Views
6K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K