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>

Réponses (2)

jonas
jonas le 2 Août 2018
Modifié(e) : jonas le 2 Août 2018
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 commentaires

Hi, It is showing the error like
jonas
jonas le 2 Août 2018
Modifié(e) : jonas le 2 Août 2018
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 :)

Connectez-vous pour commenter.

Peter Perkins
Peter Perkins le 3 Août 2018
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 commentaires

I'm using 2014 version
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
jonas
jonas le 20 Août 2018
Modifié(e) : jonas le 20 Août 2018
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..
jonas
jonas le 20 Août 2018
Modifié(e) : jonas le 20 Août 2018
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.

Connectez-vous pour commenter.

Commenté :

le 21 Août 2018

Community Treasure Hunt

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

Start Hunting!

Translated by