Plot magnitude on y-axis and time on x-axis read from an excel file
Show older comments
I have the data as follows :
Stage Magnitude Date Time
1 -1 06/10/2012 17:56:50
1 -3 06/10/2012 17:59:33
2 -2 06/11/2012 8:52:45
2 -5 06/11/2012 8:52:46
3 -3 06/12/2012 9:11:37
3 -4 06/12/2012 9:55:56
3 -1 06/12/2013 9:57:46
4 -5 06/12/2013 10:47:49
4 -2 06/12/2013 10:48:08
4 -4 06/12/2013 10:50:35
5 -3 06/12/2013 7:47:43
5 -1 06/12/2013 8:15:30
5 -2 06/12/2013 8:16:04
I want to plot the magnitude column on y-axis and date & time on x-axis. And I want to create such plots for each Stage (as per above data I should get 5 different plots)
Can I any one tell me the format of the code?
P.S : As you can see the date and time are in serial order, I mean after 11th comes 12th june(date)and after 17:59:33 after 10th june, 8:52:45 is on 11th june
1 Comment
[num, txt, raw] = xlsread('file_name');
Answers (3)
Ashish Gudla
on 8 Aug 2014
If you can import the excel file as column vectors, you can just loop through unique stage values and plot the respective Magnitude and Date/Time Values.
You may need to set the "XTick" and "XTickLabel" properties to make the plot look better.
Assuming you need to plot in 5 different figures (you can modify it to plot on different axes in same figure or whatever your requirement is) you could do something like this.
s = unique(Stage);
for i = s'
XData = Time(Stage == i);
YData = Magnitude(Stage == i);
DData = Date(Stage == i);
figure;
plot(XData , YData);
set(gca , 'XTick', XData,'XTickLabel', [datestr(DData) datestr(XData,'HH:MM:SS')]);
end
Nir Rattner
on 8 Aug 2014
You can use the Import Data tool in the Home tab to import your data into MATLAB. At that point you can use the "datenum" function to combine the dates and times and then use the "datetick" function to properly display the dates and times on your plots.
for i = Stage(1):Stage(end)
figure;
stageIndex = Stage == i;
DateTime = datenum(Date(stageIndex)) + datenum(Time(stageIndex)) - datenum('00:00');
plot(DateTime, Magnitude(stageIndex), '-o');
datetick('x', 0);
end
1 Comment
Michael Haderlein
on 12 Aug 2014
Please try this:
[num,txt]=xlsread('test.xlsx');
figure
dates=datestr(datenum(txt(2:end,end-1),'mm.dd.yyyy')+num(:,end),'dd.mm.yyyy HH:MM:SS');
for cnt=1:max(num(:,1))
subplot(max(num(:,1)),1,cnt)
plot(num(num(:,1)==cnt,2))
set(gca,'xtick',1:sum(num(:,1)==cnt),'xticklabel',dates(num(:,1)==cnt,:))
end
On my computer, Excel changed the date format from mm/dd/yyyy automatically to mm.dd.yyyy. So maybe you need to change this in the dates line.
8 Comments
KRUNAL
on 12 Aug 2014
KRUNAL
on 12 Aug 2014
KRUNAL
on 12 Aug 2014
Michael Haderlein
on 13 Aug 2014
This line is converting the date and the time to one date&time information by adding them. In case your Excel installation shows the date in format mm/dd/yyyy, please change the first format definition in this line. Also, for me the mm/dd/yyyy format is very inconvenient, so I'd rather use dd.mm.yyyy as xticklabel. If you want to keep the mm/dd/yyyy format, also change the second format definition in this line.
KRUNAL
on 13 Aug 2014
Michael Haderlein
on 13 Aug 2014
I don't have much time right now, but if you can use my answer from above, you just need to replace the line with the subplot by "figure". Then you'll plot each stage in an individual figure.
KRUNAL
on 13 Aug 2014
Michael Haderlein
on 14 Aug 2014
I don't understand you code in every detail. If you use my code, change both "dd.mm.yyyy" to "mm/dd/yyyy", what will be the error? If you want, you can also change "subplot(max(num(:,1)),1,cnt)" to simply "figure".
Categories
Find more on Dates and Time 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!