Data on Y-axis, Date & time on X-axis
1 view (last 30 days)
Show older comments
As per the suggestion of Ashish Gudia's post on date 8 Aug 2014 on one of my question's link
I manage to do try and get the data. But still I am having issues in reading them and plotting. Currently I am using the following code :
clc;
clear all;
orginf = '<filelocation>';
sheet = 'Sheet1';
[~,~,Stage] = xlsread (orginf,sheet,'C2:S151');
s = unique(cell2mat(Stage(:,1)));
AcquisitionDate(:,1) = (Stage(:,3));
AcquisitionTime(:,1) = cell2mat(Stage(:,4));
Magnitude(:,1) = cell2mat(Stage(:,17));
for j =1:150
for i = s'
XData(j,:)= AcquisitionDate(s == i);
YData(j,:) = Magnitude(s == i);
DData(j,:) = AcquisitionTime(s == i);
% figure;
% plot(XData,YData);
% set(gca ,'XTick', XData,'XTickLabel',[datestr(DData) datestr(XData,'HH:MM:SS')]);
end
end
When I use this code, I get correct data in AcquisitionDate,Magnitude and AcquisitionTime. But when it goes through the loop, it reads just one data from each and copies it to the 150x1 cells of XData, YData and DData respectively
3 Comments
Geoff Hayes
on 27 Aug 2014
Edited: Geoff Hayes
on 27 Aug 2014
Krunal - please see dpb's answer below.
While I realize that you have changed the above code to include j on each iteration, all that it is doing now is storing the same data 150 times. Since s doesn't depend on j there is nothing gained by the repetition.
Answers (2)
dpb
on 27 Aug 2014
Edited: dpb
on 28 Aug 2014
As was said multiple times in the previous thread, use
[nums,dates] = xlsread (orginf,sheet,'C2:S151');
instead of the raw text third argument. Then you'll have the two numeric columns immediately in nums
Then convert the date/time columns to Matlab datenums via
dn=datenum(dates,'formatstring')
where the format string matches the date format as read from the Excel spreadsheet.
Then plot vs dn on the x-axis and use datetick to format. See
doc datetick
for details on it.
0 Comments
dpb
on 27 Aug 2014
Edited: dpb
on 28 Aug 2014
As noted above, took your data and pasted it into Excel...
>> [data,dates]=xlsread('krunal.xls');
>> dn=datenum(dates(2:end,3),'mm/dd/yyyy')+data(:,4);
>> plot(dn,data(:,2))
>> datetick('x','keeplimits')
>>
Works just fine; note that xlsread converts the time string into fractional dates automagically so just add them to the date integer values.
Only thing didn't do here was to segregate by the Stage since there are so few datapoints; have to "leave something for the student"... :)
On reflection, perhaps the segregation is the part that is giving trouble -- one relatively simple way to do this kind of thing in general --
u=unique(data(:,1)); % get the unique values in the Stage vector
ix=data(:,1)==u(1); % find the first ones
plot(dn(ix),data(ix,2)) % and plot them
hold on % we'll add the others to the same axes this time
for i=2:length(u) % so process the remainder after the first...
ix=(data(:,1)==u(i));
plot(dn(ix),data(ix,2),'color',clr{i,:});
end
legend(num2str(u,'Stage %d')) % identify them
datetick('x','keeplimits') % turn on date markings
xlim([datenum('1-Mar-2012') datenum('1-Oct-2013')]) % stretch out axes some
I got the default color vector clr() used above by the simple expedient of plotting some value and then retrieving the colors from the default plot. I did that by
hL=plot(rand(2,5)); % dummy plot of five lines; save handles
clr=get(hL,'color'); % retrieve the color vectors in a cell array
You could also use the predefined color name strings or generate your own specific colors.
If you want separate plots just remove the hold on after the first; if you want multiple figures add figure in the loop; for something like this subplot might be of interest...
0 Comments
See Also
Categories
Find more on Calendar 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!