How to plot date and time on x axis and data on y axis using matlab?

Excel file is attatched and also code is given below,
Excel_data=xlsread('Excel_Worksheet.xlsx');
column_1= Excel_data(:,1);
column_2= Excel_data(:,2);
numex=numel(column_1);
Excel_data1='Excel_Worksheet.xlsx';
sheet = 1;
xlRange = 'D3';
for i=1:numex
finalresult(i)=column_1(i)+column_2(i);
xlswrite(Excel_data1,finalresult',sheet,xlRange)
end
[NUM TEXT RAW]=xlsread('Excel_Worksheet.xlsx');
H1=RAW(:,1);
H2=RAW(:,2);
plot(H1,H2);
On excecuting, it is showing an error like </matlabcentral/answers/uploaded_files/127407/Capture.JPG>

Answers (2)

The data import is all messy and you are trying to jam two cell arrays into the plot. Two options, either add the cell range of the data (excluding headers) after the xlsread.
[num,str]=xlsread('Excel_Worksheet.xlsx','A3:B100')
dates=datenum(str(:,1))
plot(dates,num)
datetick('x','HH:MM','keeplimits','keepticks')
Better yet, use readtable instead of xlsread. After the loop, replace your code by these lines
opts = detectImportOptions('Excel_Worksheet.xlsx','NumHeaderLines',2);
data=readtable('Excel_Worksheet.xlsx',opts,'ReadVariableNames',true);
plot(data{:,1},data{:,2});

4 Comments

Are you perhaps running a MATLAB release older than 2016b?
I just fixed a typo in the first method, it should work now (also for older MATLAB versions).
You're welcome. Feel free to accept the answer if it was helpful :)

Sign in to comment.

As Jonas suggests, if you are using a recent version of MATLAB, use readtable, and you should be able to just plot the resulting datetime values.

6 Comments

Did you try the first option I gave you?
ys..i tried ...
Excel_data=xlsread('Excel_Worksheet.xlsx');
column_1= Excel_data(:,1);
column_2= Excel_data(:,2);
numex=numel(column_1);
Excel_data1='Excel_Worksheet.xlsx';
sheet = 1;
xlRange = 'D3';
for i=1:numex
% finalresult(i)=column_1(i).*column_2(i);
finalresult(i)=column_1(i)+column_2(i);
xlswrite(Excel_data1,finalresult',sheet,xlRange)
end
[num,str]=xlsread('Excel_Worksheet.xlsx','A3:D50')
dates=datenum(str(:,1))
plot(dates,num(:,3));
datetick('x','dd/mm/yyyy HH:MM','keeplimits','keepticks')
the actual Excel_worksheet is given
ERROR!!!!! but it is not reading the all the date values (str.mat) from the excel
I don't understand what you are talking about. It is reading all values just fine. Exactly what value is missing? You do know the difference between AM and PM right?
If you mean that it is not reading columns B-D, that is because those are not STRINGS. They are instead stored in the NUM output. You can find ALL columns in the third output of xlsread:
[num,str,raw]=xlsread('Excel_Worksheet.xlsx','A3:D50')
Please continue this discussion on XLSREAD in the other answer.
Sir, just look at the excel sheet...the fifth cell time is 1:30:00..but in the readed str.mat it is 2:30:00 am..
Well, of course they are not synced row-wise. You start reading from A3, so the third row in the excel file will be the first row in the imported data variable..... The last rows are 100% the same, and the number of rows are also the same. So no value is missing. Case closed.
I will be replying in the other answer from now on, please do the same.

Sign in to comment.

Asked:

on 2 Aug 2018

Commented:

on 21 Aug 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!