Excel date column export into Matlab

In summary, the conversation discusses using MATLAB to plot data from an Excel file, and encountering an error due to the date column not being read correctly. The solution is to use the datenum function to convert the date column, and then use the datestr function to format the axis labels.
  • #1
adeeyo
20
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
  • #2
http://www.mathworks.com/help/matlab/import_export/when-to-convert-dates-from-excel-files.html
 
  • #3
As a workaround, what happens if you add an empty column in the spreadsheet?
 
  • #4
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
 
  • #5
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
 

FAQ: Excel date column export into Matlab

1. How do I export a date column from Excel into Matlab?

To export a date column from Excel into Matlab, you can use the "xlsread" function in Matlab. This function allows you to read data from an Excel spreadsheet and import it into Matlab. Simply specify the file path and name of the Excel file, and then specify the range of cells containing your date column. Matlab will automatically convert the dates into serial date numbers, which can be used for further analysis.

2. Can I export multiple date columns from Excel into Matlab?

Yes, you can export multiple date columns from Excel into Matlab by specifying the range of cells for each column in the "xlsread" function. Just make sure that the data in each column is consistent and in the same format, so that Matlab can convert it correctly.

3. How do I handle missing or invalid dates when exporting from Excel to Matlab?

If there are missing or invalid dates in your Excel date column, Matlab will automatically convert them into a "NaN" value. This indicates that the date is not a valid serial date number. You can either remove these values from your data, or use the "isnan" function in Matlab to handle them in your analysis.

4. Can I export date columns with different formats from Excel into Matlab?

Yes, you can export date columns with different formats from Excel into Matlab. However, it is important to specify the correct format in the "xlsread" function so that Matlab can convert the dates correctly. If the format is not specified, Matlab will use the default format, which may not be accurate for your data.

5. How can I convert serial date numbers back into date formats in Matlab?

To convert serial date numbers back into date formats in Matlab, you can use the "datestr" function. This function allows you to specify the format in which you want the date to be displayed. You can also use the "datenum" function to convert date strings into serial date numbers in Matlab.

Similar threads

Replies
8
Views
834
Replies
2
Views
3K
Replies
11
Views
4K
Replies
18
Views
5K
Replies
2
Views
1K
Replies
4
Views
3K
Replies
4
Views
28K
Back
Top