Plot magnitude on y-axis and time on x-axis read from an excel file

3 views (last 30 days)
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
Andy L
Andy L on 12 Aug 2014
Edited: Andy L on 12 Aug 2014
Have you read the data in fine? See xlsread if you are struggling!
[num, txt, raw] = xlsread('file_name');

Sign in to comment.

Answers (3)

Ashish Gudla
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
  4 Comments
KRUNAL
KRUNAL on 12 Aug 2014
I did it by entering the statement :
dt_time_new=cell2mat(dt_time);
plot(dt_time_new,mag,'--rs','LineWidth',2,...
'MarkerEdgeColor','k',...
'MarkerFaceColor','g',...
'MarkerSize',10)
These two lines I have added after
mag = xlsread........
line It shows me now error as : Cannot support cell arrays containing cell arrays or objects for the cell2mat line
KRUNAL
KRUNAL on 27 Aug 2014
Edited: KRUNAL on 27 Aug 2014
@Ashish... This is what I wrote to access the column data. In my file stage is in column C, Date(in file it is called as AcquisitionDate)in column E , Time(in file it is called as AcquisitionTime)in column F and magnitude in column S
clc;
clear all;
orginf = '<filelocation>;
sheet = 'Sheet1';
Stage = xlsread (orginf,sheet,'C1:S151');
s = unique(Stage); %line 9%
for i = s'
XData = AcquisitionDate(Stage == i); %line 11%
YData = Magnitude(Stage == i);
DData = AcquisitionTime(Stage == i);
figure;
plot(DData,YData);
set(gca ,'XTick', XData,'XTickLabel',[datestr(DData) datestr(XData,'HH:MM:SS')]);
end
It gives me error as
Undefined function 'AcquisitionDate' for input arguments of type 'logical'.
Error in event_plot_tr (line 11) XData = AcquisitionDate(Stage == i);
I also tried to change the line of Stage = xlsread.... to
[~, ~, Stage] = xlsread(orginf,sheet,'C1:S151');
for which I received the error as
Input A must be a cell array of strings.
Error in event_plot_tr (line 9) s = unique(Stage);
Tell me where I am wrong?

Sign in to comment.


Nir Rattner
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
KRUNAL
KRUNAL on 12 Aug 2014
Edited: KRUNAL on 12 Aug 2014
I am trying to import data but it is not importing correct data. I wrote your above data and using the import function I tried importing data column wise.
The error says
Undefined function 'Stage' for input arguments of type 'double'.
Also after importing the data I am not able to see them in the workspace soon after I hit the run coomand

Sign in to comment.


Michael Haderlein
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
KRUNAL on 13 Aug 2014
I tried using your answer in my code as I mentioned above and I also told you what was the error. I would be glad if you can tell me where I am going wrong in that
Michael Haderlein
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".

Sign in to comment.

Categories

Find more on Programming 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!