Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
date search and giving respective value

Subject: date search and giving respective value

From: sensation

Date: 4 Apr, 2013 13:47:08

Message: 1 of 11

Hi there,
i have date (hourly data) in my file in matlab serial data number and i am searching for a date I want as:

idents1 = find(data1(:,1)>=datenum([2004 01 01 00 00 00]) & data1(:,1)<=datenum([2008 12 31 23 00 00]));

tspen = length(idents1)


for i=1 to tspen

NOW I AM FACING THE PROBLEM

HOW TO WRITE

IF EP HIGHER or equal THAN FIRST OF JANUARY AND LESS THAN 1 OF APRIL (OF EACH YEAR) EP HAS A VALUE OF 0.62

IF HIGHER OR EQUAL FIRST OF APRIL AND LESS THEN 15 OF APRIL, EP SHOULD INTERPOLATE VALUES FROM 0.62 TO 0.97 UNTIL 14 OF APRIL
(E.G. value of ep for 14 of april at 23 h should be a bit less than 0.97)

if higher or equal 15 of april and less than 01 November ep has a value of 0.97.

if equal or higher than 1 november and less then 15 november, ep should interpolate values from 0.97 to 0.75
(14november 23 h should have value a bit higher than 0.75).

if higher or equal the 15 november and less then 1 of january ep=0.75

data(:,1)= ep*y^2;

end

Subject: date search and giving respective value

From: dpb

Date: 4 Apr, 2013 14:39:21

Message: 2 of 11

On 4/4/2013 8:47 AM, sensation wrote:
> Hi there, i have date (hourly data) in my file in matlab serial data
> number and i am searching for a date I want as:
>
> idents1 = find(data1(:,1)>=datenum([2004 01 01 00 00 00]) & ...
> data1(:,1)<=datenum([2008 12 31 23 00 00]));
> tspen = length(idents1)
>
>
> for i=1 to tspen
>
> NOW I AM FACING THE PROBLEM
>
> HOW TO WRITE
> IF EP HIGHER or equal THAN FIRST OF JANUARY AND LESS THAN 1 OF APRIL (OF
> EACH YEAR) EP HAS A VALUE OF 0.62

What's EP? It doesn't seem to make any sense to set some variable w/
the value of a date (between Jan 1 and March 30) to a fraction like 0.6

...

All the rest seem as though would be solvable by an extension of what
need for the above so let's try to figure out what it is you're really
asking here.

How about a (very short) sample dataset? Shouldn't take but a
half-dozen to ten records to illustrate...

--

Subject: date search and giving respective value

From: sensation

Date: 5 Apr, 2013 09:34:16

Message: 3 of 11

dpb <none@non.net> wrote in message <kjk3ao$n2d$1@speranza.aioe.org>...
> On 4/4/2013 8:47 AM, sensation wrote:
> > Hi there, i have date (hourly data) in my file in matlab serial data
> > number and i am searching for a date I want as:
> >
> > idents1 = find(data1(:,1)>=datenum([2004 01 01 00 00 00]) & ...
> > data1(:,1)<=datenum([2008 12 31 23 00 00]));
> > tspen = length(idents1)
> >
> >
> > for i=1 to tspen
> >
> > NOW I AM FACING THE PROBLEM
> >
> > HOW TO WRITE
> > IF EP HIGHER or equal THAN FIRST OF JANUARY AND LESS THAN 1 OF APRIL (OF
> > EACH YEAR) EP HAS A VALUE OF 0.62
>
> What's EP? It doesn't seem to make any sense to set some variable w/
> the value of a date (between Jan 1 and March 30) to a fraction like 0.6
>
> ...
>
> All the rest seem as though would be solvable by an extension of what
> need for the above so let's try to figure out what it is you're really
> asking here.
>
> How about a (very short) sample dataset? Shouldn't take but a
> half-dozen to ten records to illustrate...
>
> --


to be more clear I have data set of 8 years and have to calculate evapotranspiration by formula= etp(of that hour) * ep (it is just a coefficient) but for each period ep shoul be different depending on a season.

so the aime is to have in a result sth like lets say:


%setting up the time period I want
data(:,1)=idents1 = find(data1(:,1)>=datenum([2004 01 01 00 00 00]) & ...
            data1(:,1)<=datenum([2008 12 31 23 00 00]));
 tspen = length(idents1)

 for i=1 to tspen
......

e.g.

matlab data data(:,2)

730486.0000 0.0505
730486.0417 0.0504
730486.0833 0.0502
730486.1250 0.0501 here ep should be 0.62 (from January first to March 31)


730486.1667 0.0499
730486.2083 0.0498
730486.2500 0.0496
730486.2917 0.0500
730486.3333 0.0506 here ep should be 0.97 (From 15 April until 31 October)



730486.3750 0.0511
730486.4167 0.0517
730486.4583 0.0522
730486.5000 0.0528
730486.5417 0.0534
730486.5833 0.0539 here ep should be 0.75 (From 15 November until 31 December)



HOWEVER between period of 1 april to 15 april ep should gradually raise until reaching 0.97 and from 1 November until 15 november ep
should gradually dropping until reaching 0.75.


and then for each year ep sholud take its respective values and caluclate:



  etp(:,1)= ep*data(:,2)^2; where data(:,2) are etp known values

Subject: date search and giving respective value

From: dpb

Date: 5 Apr, 2013 17:56:36

Message: 4 of 11

On 4/5/2013 4:34 AM, sensation wrote:
> dpb <none@non.net> wrote in message <kjk3ao$n2d$1@speranza.aioe.org>...
>> On 4/4/2013 8:47 AM, sensation wrote:
...
>> > HOW TO WRITE
>> > IF EP HIGHER or equal THAN FIRST OF JANUARY AND LESS THAN 1 OF APRIL
>> (OF EACH YEAR) EP HAS A VALUE OF 0.62
>>
>> What's EP? It doesn't seem to make any sense to set some variable w/
>> the value of a date (between Jan 1 and March 30) to a fraction like 0.6
...
> to be more clear I have data set of 8 years and have to calculate
> evapotranspiration by formula= etp(of that hour) * ep (it is just a
> coefficient) but for each period ep shoul be different depending on a
> season.
>
> so the aime is to have in a result sth like lets say:
>
> %setting up the time period I want
> idents1 = find(data1(:,1)>=datenum([2004 01 01 00 00 00]) & ...
> data1(:,1)<=datenum([2008 12 31 23 00 00]));
> tspen = length(idents1)
>
> for i=1 to tspen
> ......
>
> e.g.
> matlab data data(:,2)
>
> 730486.0000 0.0505
> 730486.0417 0.0504
> 730486.0833 0.0502
> 730486.1250 0.0501 here ep should be 0.62 (from January first to March 31)
>
>
> 730486.1667 0.0499
> 730486.2083 0.0498
> 730486.2500 0.0496
> 730486.2917 0.0500
> 730486.3333 0.0506 here ep should be 0.97 (From 15 April until 31 October)
>
>
>
> 730486.3750 0.0511
> 730486.4167 0.0517
> 730486.4583 0.0522
> 730486.5000 0.0528
> 730486.5417 0.0534
> 730486.5833 0.0539 here ep should be 0.75 (From 15 November until 31
> December)
>
>
>
> HOWEVER between period of 1 april to 15 april ep should gradually raise
> until reaching 0.97 and from 1 November until 15 november ep should
> gradually dropping until reaching 0.75.
>
>
> and then for each year ep sholud take its respective values and caluclate:
>
>
> etp(:,1)= ep*data(:,2)^2; where data(:,2) are etp known values

OK, that's better...start w/ the coefficient. If I get it correctly,
your functional form for ep is as follows...

01Jan 0.62
31Mar 0.62
15Apr 0.97
31Oct 0.97
15Nov 0.75
31Dec 0.75

w/ interpolated values between the breakpoints--let's assume linear for
the moment; you can change form as desired or convert to some functional
form if have that or to (say) a spline to create smooth derivative if
that were to be important.

So build your interpolation table -- sotoo of

ep_func = [0.62 0.62 0.97 0.97 0.75 0.75]'; % values
d_ep={'01Jan' '31Mar' '15Apr' '31Oct' '15Nov' '31Dec'}'; % days
y_zero = 2000; % say--an arbitrary starting year
y_ep=cellstr(repmat(num2str(y_zero),size(d_ep)));
% now get datenums for days into year corresponding to d_ep
dn_ep=datenum(strcat(d_ep,y_ep))-datenum(y_zero,1,1,0,0,0);

Now you've got the interpolating table set up; go to the data selection
part....I'm going to just use 'd' as your data date vector and 'y' for
the data you're wanting to get for brevity

dstrt=datenum(2004,1,1,0,0,0);
dend =datenum(2008,12,31,23,0,0);
idx=isbetween(d,dstrt,dend);

Oh, wait...that's a little awkward...the interpolating table is only for
a year. OK, simplest fix is to do it by year in a loop setting dstrt
and end for the appropriate years on each pass. _BUT_, if you can stand
the approximation for a leap year (or fix up the interpolating table to
be for a leap year and just accept the roundoff or the others), you can
simply use modulo 365 arithmetic and go on and give the extrapolating
value for the 366th day.

But, I also just noted you have a pretty major discontinuity in the
table values you've given when wraparound from one year to the next.
The value of ep on 31Dec is given as 0.75 and as 0.62 on 1Jan. That
seems questionable if you care about interpolating smoothly on the other
intervals within the year. That, obviously, is not a Matlab question
but one of what your model should do.

So, anyway, lets continue...

etp = interp1(dn_ep,y_ep,mod(d(idx),365),'linear',y_ep(end)).*y.^2;

NB: untested; salt to suit...

--

Subject: date search and giving respective value

From: dpb

Date: 5 Apr, 2013 19:41:57

Message: 5 of 11

On 4/5/2013 12:56 PM, dpb wrote:
...

> Now you've got the interpolating table set up; go to the data selection
> part....I'm going to just use 'd' as your data date vector and 'y' for
> the data you're wanting to get for brevity
>
> dstrt=datenum(2004,1,1,0,0,0);
> dend =datenum(2008,12,31,23,0,0);
> idx=isbetween(d,dstrt,dend);
>
...

Drat, I forgot the name of my own helper function as well providing the
definition -- it's been very useful in reducing complexity quite a few
times here recently it seems...

idx=iswithin(d,dstrt,dend);

where

function flg=iswithin(x,lo,hi)
   % Returns logical array size(x) for x within [lo-hi], inclusive

   flg = (x>=lo) & (x<=hi);

--

Subject: date search and giving respective value

From: dpb

Date: 5 Apr, 2013 20:17:27

Message: 6 of 11

On 4/5/2013 12:56 PM, dpb wrote:
...

> So build your interpolation table -- sotoo of
>
> ep_func = [0.62 0.62 0.97 0.97 0.75 0.75]'; % values
> d_ep={'01Jan' '31Mar' '15Apr' '31Oct' '15Nov' '31Dec'}'; % days
> y_zero = 2000; % say--an arbitrary starting year
> y_ep=cellstr(repmat(num2str(y_zero),size(d_ep)));
> % now get datenums for days into year corresponding to d_ep
> dn_ep=datenum(strcat(d_ep,y_ep))-datenum(y_zero,1,1,0,0,0);
>
> Now you've got the interpolating table set up; go to the data selection
> part....I'm going to just use 'd' as your data date vector and 'y' for
> the data you're wanting to get for brevity
>
> dstrt=datenum(2004,1,1,0,0,0);
> dend =datenum(2008,12,31,23,0,0);
> idx=isbetween(d,dstrt,dend);
>
...

> etp = interp1(dn_ep,y_ep,mod(d(idx),365),'linear',y_ep(end)).*y.^2;
>
> NB: untested; salt to suit...

Yeah, as well as the previous, forgot the logical addressing on the y...

etp = interp1(dn_ep,y_ep,mod(d(idx),365),'linear',y_ep(end)).*y(idx).^2;

--

Subject: date search and giving respective value

From: sensation

Date: 18 Apr, 2013 15:57:09

Message: 7 of 11

dpb <none@non.net> wrote in message <kjnbgm$c10$1@speranza.aioe.org>...
> On 4/5/2013 12:56 PM, dpb wrote:
> ...
>
> > So build your interpolation table -- sotoo of
> >
> > ep_func = [0.62 0.62 0.97 0.97 0.75 0.75]'; % values
> > d_ep={'01Jan' '31Mar' '15Apr' '31Oct' '15Nov' '31Dec'}'; % days
> > y_zero = 2000; % say--an arbitrary starting year
> > y_ep=cellstr(repmat(num2str(y_zero),size(d_ep)));
> > % now get datenums for days into year corresponding to d_ep
> > dn_ep=datenum(strcat(d_ep,y_ep))-datenum(y_zero,1,1,0,0,0);
> >
> > Now you've got the interpolating table set up; go to the data selection
> > part....I'm going to just use 'd' as your data date vector and 'y' for
> > the data you're wanting to get for brevity
> >
> > dstrt=datenum(2004,1,1,0,0,0);
> > dend =datenum(2008,12,31,23,0,0);
> > idx=isbetween(d,dstrt,dend);
> >
> ...
>
> > etp = interp1(dn_ep,y_ep,mod(d(idx),365),'linear',y_ep(end)).*y.^2;
> >
> > NB: untested; salt to suit...
>
> Yeah, as well as the previous, forgot the logical addressing on the y...
>
> etp = interp1(dn_ep,y_ep,mod(d(idx),365),'linear',y_ep(end)).*y(idx).^2;
>
> --


hi, I cleaned up a bit a code but i am getting error of
"Subscripted assignment dimension mismatch" at line 13.

so i am using code like this:


data1=load('.....'); % loading data sets where first column is date vector, second, third etc are different weight vectors

    d=data1(:,1); % date vector as matlab date
    y=data1(:,2); %weight
    dstrt=datenum(2004,1,1,0,0,0);
    dend =datenum(2008,12,31,23,0,0);
    idx=iswithin(d,dstrt,dend); %following your function iswithin

    tspen=length(idx);

    for a= 2 %I have a loop for the entire model where 2 equals to second vector set, 3 to third etc
      
    data(:,1)=data1(idx,1); % time step
    data(:,2)=data1(idx,a); % weight
   
    ep_func = [0.62 0.62 0.97 0.97 0.75 0.75]'; % values
    
    d_ep={'01Jan' '31Mar' '15Apr' '31Oct' '15Nov' '31Dec'}'; % days
    
    y_zero = 2004; % say--an arbitrary starting year
    
    y_ep=cellstr(repmat(num2str(y_zero),size(d_ep)));
    
    % now get datenums for days into year corresponding to d_ep
    dn_ep=datenum(strcat(d_ep,y_ep))-datenum(y_zero,1,1,0,0,0);
    
    for n=1 to tspen;
    
    etp(:,1) = interp1(dn_ep,y_ep,mod(d,d(idx),365),'linear',y_ep(end)).*y(idx).^2;
    
    end
  
    save('etp','etp',-ASCII);
    
    end

cheers

Subject: date search and giving respective value

From: dpb

Date: 18 Apr, 2013 16:26:58

Message: 8 of 11

On 4/18/2013 10:57 AM, sensation wrote:
...

> hi, I cleaned up a bit a code but i am getting error of "Subscripted
> assignment dimension mismatch" at line 13.
>
> so i am using code like this:
>
> data1=load('.....'); % loading data sets where first column is date
> vector, second, third etc are different weight vectors
...

Paste the actual error and context instead of making go thru and try to
figure it out where that might actually be after newsreader messes up
linewrap, etc., ...

Or, use debug and look at results of the LHS and RHS and see where your
indexing expression mismatch comes from...

--

Subject: date search and giving respective value

From: sensation

Date: 19 Apr, 2013 09:58:09

Message: 9 of 11

dpb <none@non.net> wrote in message <kkp6sq$7dn$1@speranza.aioe.org>...
> On 4/18/2013 10:57 AM, sensation wrote:
> ...
>
> > hi, I cleaned up a bit a code but i am getting error of "Subscripted
> > assignment dimension mismatch" at line 13.
> >
> > so i am using code like this:
> >
> > data1=load('.....'); % loading data sets where first column is date
> > vector, second, third etc are different weight vectors
> ...
>
> Paste the actual error and context instead of making go thru and try to
> figure it out where that might actually be after newsreader messes up
> linewrap, etc., ...
>
> Or, use debug and look at results of the LHS and RHS and see where your
> indexing expression mismatch comes from...
>
> --


hi,

i am still getting error that sth with interp1 function does not work well regarding input parmeters:

lets say as you proposed we take:


ep_func = [0.62 0.62 0.97 0.97 0.75 0.75]'; % values
    
d_ep={'01Jan' '31Mar' '15Apr' '31Oct' '15Nov' '31Dec'}'; % days

y_zero = 2000; % say--an arbitrary starting year
    
y_ep=cellstr(repmat(num2str(y_zero),size(d_ep)));

 d=data1(1:4,1); % date vector as matlab date giving e.g. for 4 hours

   7.304860000000000
   7.304860417000000
   7.304860833000000
   7.304861250000000

    y=2.5 ;
    
    dstrt=datenum(2000,1,1,0,0,0); % lets use also here 4 hours
    dend =datenum(2000,1,1,4,0,0);
    idx=iswithin(d,dstrt,dend);

 etp(:,1) = interp1(dn_ep,y_ep,mod(d(idx),365),'linear',y_ep(end)).*y(idx);

results should look like: (since it is january it should take values of 0.62 for each hour)

etp [1,1]= 0.62 * 2.5;
etp [2,1]=0.62 * 2.5;
etp [2,1]=0.62 * 2.5;
etp [2,1]=0.62 * 2.5;

hope this explains a bit more ,

thanks

m.

Subject: date search and giving respective value

From: dpb

Date: 19 Apr, 2013 14:29:23

Message: 10 of 11

On 4/19/2013 4:58 AM, sensation wrote:
...

> i am still getting error that sth with interp1 function does not work
> well regarding input parmeters:
>
> lets say as you proposed we take:
>
>
> ep_func = [0.62 0.62 0.97 0.97 0.75 0.75]'; % values
> d_ep={'01Jan' '31Mar' '15Apr' '31Oct' '15Nov' '31Dec'}'; % days
>
> y_zero = 2000; % say--an arbitrary starting year
> y_ep=cellstr(repmat(num2str(y_zero),size(d_ep)));
>
> d=data1(1:4,1); % date vector as matlab date giving e.g. for 4 hours
>
> 7.304860000000000
> 7.304860417000000
> 7.304860833000000
> 7.304861250000000

Those aren't valid Matlab datenums -- they are lacking precision. This
has been noted before that you may have some resolution problems. Also
the magnitude is off--if the integer portion is DOY, that's ok; DATESTR
and friends can handle it but you'll need to use proper formatting to
interpret it correctly. As example of the resolution...'tn' is the
values above pasted at command line--

 >> datestr(tn,'HH:MM:SS.FFF')
ans =
07:18:59.904
07:18:59.940
07:18:59.976
07:19:00.012
 >> datestr(mod(tn,fix(tn)),'HH:MM:SS.FFF')
ans =
07:18:59.904
07:18:59.940
07:18:59.976
07:19:00.012
 >>

NB that the datestr format ignores the day...

> y=2.5 ;
> dstrt=datenum(2000,1,1,0,0,0); % lets use also here 4 hours
> dend =datenum(2000,1,1,4,0,0);
> idx=iswithin(d,dstrt,dend);
> etp(:,1) = interp1(dn_ep,y_ep,mod(d(idx),365),'linear',y_ep(end)).*y(idx);
>
...

Looks like you're trying to copy too much instead of reading the code to
follow what it was doing (or intending to do). I used the wrong
variable name for the interpolant above--y_ep is the year vector used to
build the table, not the function values needed. Sorry, I didn't notice
that error in the errata posted earlier. (As noted, I said it was
"untested" :) )

Here's a sample usage of the interpolating function--

 >> d=[0:15:500]';
 >> dn=datenum(y-zero,1,d',0,0,0)-datenum(y_zero,0,0,0,0,0);
 >> etp=interp1(dn_ep,ep_func,mod(dn,365),'linear',ep_func(end));
 >> plot(dn,etp)
 >> datetick('x','mmmdd','keepticks','keeplimits')
 >>

This plots the interpolated function over a year-plus number of days.
Note it needs the days from beginning of year as the 'x' or time input
hence the y_zero in both cases above in dn. You will use the y_zero of
your initial data date--the point of the modulo function is to wrap
around multiple years given days>365 going into the interpolating function.

It won't care about the hours/days; it'll be as accurate in the
interpolation as the input data are. For presentation purposes the
single precision likely won't hurt at all; if you were trying to do
something like frequency estimation it could bite big-time.

Also remember the "problem" of the discontinuity from the values you
gave for 31Dec and the following 1Jan--I've no clue if it really is
wanted as so or not, of course, just bringing it to your attention.

--

Subject: date search and giving respective value

From: dpb

Date: 19 Apr, 2013 17:16:10

Message: 11 of 11

On 4/19/2013 9:29 AM, dpb wrote:
...

>> d=data1(1:4,1); % date vector as matlab date giving e.g. for 4 hours
>>
>> 7.304860000000000
>> 7.304860417000000
>> 7.304860833000000
>> 7.304861250000000
>
> Those aren't valid Matlab datenums -- they are lacking precision. This
> has been noted before that you may have some resolution problems. Also
> the magnitude is off--if the integer portion is DOY, that's ok; DATESTR
> and friends can handle it but you'll need to use proper formatting to
> interpret it correctly. As example of the resolution...'tn' is the
> values above pasted at command line--
>
...

Just realized that the above were pasted w/o the column 1E5 multiplier
so the 7 isn't as funky as it looks...

 >> datestr(tn*1E5,'ddmmmyyyy HH:MM:SS.FFF')
ans =
01Jan2000 00:00:00.000
01Jan2000 01:00:02.880
01Jan2000 01:59:57.120
01Jan2000 03:00:00.000
 >>

Still some precision roundoff indications???

--

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us