How to plot date and time on x axis and data on y axis using matlab?
Show older comments
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
amrutha raj
on 2 Aug 2018
amrutha raj
on 21 Aug 2018
jonas
on 21 Aug 2018
You're welcome. Feel free to accept the answer if it was helpful :)
Peter Perkins
on 3 Aug 2018
0 votes
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
amrutha raj
on 6 Aug 2018
jonas
on 6 Aug 2018
Did you try the first option I gave you?
amrutha raj
on 20 Aug 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.
amrutha raj
on 20 Aug 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.
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

