Shahgee
Shahgee

Reputation: 3405

label a figure with date (only once for a day) , matlab

I'm trying to plot values in matlab.

my csv file looks like this,

> 15.12.2012 11:27; 0.9884753
> 15.12.2012 11:12; 10.670.642
> 15.12.2012 10:57; 114.455.145
> 15.12.2012 10:42; 101.301.446
> 14.12.2012 10:27; 0.99031037
> 14.12.2012 10:12; 104.594.388
> 14.12.2012 09:57; 0.97192177
> 14.12.2012 09:42; 0.8925
> 14.12.2012 09:27; 0.8985693
> 14.12.2012 09:12; 0.955
> 14.12.2012 08:57; 0.95103529
> 13.12.2012 08:42; 0.95203444
> 13.12.2012 08:27; 0.955
> 13.12.2012 08:12; 0.95970876
> 13.12.2012 07:57; 0.95929422
> 13.12.2012 07:42; 0.95578656
> 13.12.2012 07:27; 0.955
> 12.12.2012 07:12; 0.955
> 12.12.2012 06:57; 0.95342687
> 12.12.2012 06:42; 0.955
> 12.12.2012 06:27; 0.955
> 12.12.2012 06:12; 0.95930485
> 11.12.2012 05:57; 0.95530825
> 11.12.2012 05:42; 0.96452381
> 10.12.2012 05:27; 0.9675
> 10.12.2012 05:12; 0.98778061
> 10.12.2012 04:57; 102.982.993

I 'm reading '11.12.2012 04:57'; as string and then using datvec.

[Y, M, D, H, MN, S] = datevec('String')

Just to make it clear again

Col1=String  % [Y, M, D, H, MN, S]
Col2= number % [0.9884753;...;102.982.993]

Now i want to plot second col2 with respect to first Col1 ([Y, M, d].

plot (Col2)

It works . In my example data I have more than a single value for the same day. How can I label my plot with date that appear only once per day.

regards,

Upvotes: 1

Views: 222

Answers (4)

jasxun
jasxun

Reputation: 581

You can do something as listed below. Since the date strings are not so short, it looks nicer to render them rotated (with rotateticklabel).

str = {
  '15.12.2012 11:27';
  '15.12.2012 11:12';
  ...
  '10.12.2012 04:57'
};

num = [
  0.9884753;
  10.670642;
  ...
  102.982993
];

% find first occurrence of dates
[y,m,d,h,mn] = datevec(str,'dd.mm.yyyy HH:MM');
dn = datenum(y,m,d);
[~,ind] = unique(dn,'first');
ind = sort(ind);

% plot it nicely
plot(num);
ax = gca;
dstr = cellstr(datestr(dn,'dd.mm.yyyy'));
set(ax, 'XTick',ind, 'XTickLabel',dstr(ind), 'Position',[0.1,0.15,0.8,0.75]);
rotateticklabel(ax, 30);

And this is what you can get: enter image description here

Upvotes: 1

PearsonArtPhoto
PearsonArtPhoto

Reputation: 39718

Set up a set of custom ticks, like so:

startTicks=find([false diff(D)~=0]); % This is a bit of a hack, but will show you anytime the day has changed. Might be confused if you jump a month at a time.
plot(Col2)
set(gca,'XTick',startTicks)
set(gca,'XTickLabel',Col1(startTicks));

Upvotes: 1

Jetpac
Jetpac

Reputation: 2125

You want to use datenum in place of datevec to get a single set of date values, plot them and then use datetick. E.g. taking the last three points above:

date_str = {'10.12.2012 05:27' '10.12.2012 05:12' '10.12.2012 04:57'};
y = [0.9675 0.98778061 102.982];

x = datenum(date_str, 'dd.mm.yyyy HH:MM');

plot(x,y);
datetick x

Note: you've got numbers in the second column with two dots (e.g. "10.670.642" and "114.455.145"). Not sure what they are (thousands separators perhaps?) but you'll need to handle those. The code above won't work with those.

Upvotes: 0

s-m-e
s-m-e

Reputation: 3729

http://www.mathworks.de/de/help/matlab/ref/timeseries.plot.html

Plots of time series are what you are looking for, check the first example.

Upvotes: 0

Related Questions