2

I'm a beginner at Matlab and I need to solve a problem. First, I need to convert the UT columm from decimal hour to hour:minute:seconds. Then, I need to calculate every 5 minutes on average the ROT collumn and show the reply in a new matrix (hour:min:sec,rot mean).

Data

UT (column 1) A matrix
5.4
5.404
5.408

ROT (column2) A matrix

0.22

0.123

0.129

e.g. UT (5.404)=0.404*60=24.252; 0.252*60=15.12 ,then UT(5.404)=5:24:15 hours:min:sec

Thanks in advance

Marcelo

Oleg
  • 10,406
  • 3
  • 29
  • 57

2 Answers2

1

First convert decimal hour dates into serial dates where unity is a day:

serdates = [5.4;5.404;5.408]/24;

Then convert to string with datestr (this is however a cosmetic operation):

datestr(serdates,'HH:MM:SS')

Group observation in 5 minute bins (lb <= x < up):

ymdhms      = datevec(serdates);
[~,minbins] = histc(ymdhms(:,5),[0:5:60])

Group then by year, day, month, hour and 5 minute bins:

[untime,~,subs] = unique([ymdhms(:,1:4) minbins*5],'rows')

Accumulate rot:

rot5min = accumarray(subs,[0.22;0.123;0.129]);

And for fancier presentation collect into dataset with datestrings

dataset({ cellstr(datestr(datenum([untime,zeros(size(untime,1),1)]),31)),'Datetime'}, {rot5min 'ROT5min'})

ans = 
    Datetime                     ROT5min
    '0000-01-00 05:05:00'        0.472  
Oleg
  • 10,406
  • 3
  • 29
  • 57
  • Thanks Oleg! When a try put [~,minbins]= hist(ymdhm... command, it appears a error: Expression or statement is incorrect---possibly unbalanced { -[ (. Can I help me to solve it? – Marcelo Duarte Nov 16 '13 at 18:46
  • replace ~ with some placeholder variable like 'trash' – Oleg Nov 16 '13 at 19:49
  • Hi Oleg! Your program worked successfully! I'd like to have a question cleared up: How do I to show the "Datetime" only in hour and minutes? Why the program shows 05:05:00 instead of 05:24:00, however the ut data represents 05:24? Thanks in advance! – Marcelo Duarte Nov 18 '13 at 17:42
  • @MarceloDuarte When I group by year, day etc... it should be `minbins*5`, to correctly display which bucket we are. To change date dispaly format, change the `31` in the last call of `datestr(...,31)` to some other format or supply a custom string. You can read up ibn the help to the function which options are available. – Oleg Nov 19 '13 at 09:06
  • Hi Oleg! I've just seen that my program was without minbins*5, just minbins, I'm sorry!!!. I managed to change the date display format to 15. Thank you so much by the hints!!I really appreciate your generosity. See you! – Marcelo Duarte Nov 19 '13 at 13:45
  • Oleg, I 'm trying to make a graph from dataset but doesn't work. I tried to convert to double but doesn't work too. How to plot it ut x rot5? Thanks again . – Marcelo Duarte Nov 19 '13 at 14:39
  • @MarceloDuarte Consider posting an additional question. I cannot guess the syntax you are using and what error message you get. – Oleg Nov 20 '13 at 09:32
  • Hi Oleg! In the accumulate ROT, rot5min=accumarray(subs,[rot]), how can I calculate, instead of [rot], this new equation [sqrt(-^2)]? Thanks in advance – Marcelo Duarte Nov 29 '13 at 12:31
  • Hi Oleg! I just found out! Thanks!! – Marcelo Duarte Nov 29 '13 at 13:47
0

This will do. Cheers.

function v=myfunction(ut,rot)
% ut is a vector of decimal hours
% rot is a vector of the same length as ut
% v is a matrix with rows of the form (hour, min, sec, rot5) where
%   rot5 is an average of rot over 5 min interval from 2.5 min in the past
%   to 2.5 min to the future.

m=numel(ut);

% input validation
assert(isvector(ut) && isvector(rot) && numel(rot)==m);

% array initialization
v=zeros(m,4);
utCopy=ut;

for i=1:m
    % calculate hour from decimal hour
    v(i,1)=floor(utCopy(i));

    % calculate minute from decimal hour
    utCopy(i)=(utCopy(i)-v(i,1))*60;
    v(i,2)=floor(utCopy(i));

    % calculate second from decimal hour, round to nearest integer
    utCopy(i)=(utCopy(i)-v(i,2))*60;
    v(i,3)=round(utCopy(i));

    % calculate 5 mins central average of rot
    % to get forward average just replace the logical indexing with
    % ut>=ut(i) & ut<=ut(i)+1/12
    v(i,4)=mean(rot(ut>=ut(i)-1/24 & ut<=ut(i)+1/24));
end

end
heriantolim
  • 457
  • 2
  • 9
  • Thanks herianto 1989, I tried to use the function and assert commands but appear a error. Let me improve my question: I need to calculate every 5 minutes about this equation: ROTI=sqrt(-2), then I did, 1)rot_quad=rot.^2; 2)rot_quad_mean=mean(rot_quad); 3)rot_mean=mean(rot) and 4)rot_mean_quad=rot_mean^2 and finally ROTI=sqrt((rot_quad_mean-rot_mean_quad)). Can you help me again? – Marcelo Duarte Nov 16 '13 at 14:06
  • The function works on my computer. FYI, to use a function, save the code above into `myfunction.m`. You can then run `v=myfunction(ut,rot)` from the console window. – heriantolim Nov 16 '13 at 14:35
  • To get that `ROTI` replace line 32 (`v(i,4)=...`) with the following: `IX=ut>=ut(i)-1/24 & ut<=ut(i)+1/24;` `v(i,4)=sqrt(mean(rot(IX).^2)-mean(rot(IX))^2);` It should work. Let me know if there is any problem. – heriantolim Nov 16 '13 at 14:39
  • Also, make sure that your arguments `ut` and `rot` are of the same length. – heriantolim Nov 16 '13 at 14:44
  • Hi herianto1989, the code worked very well, v array with col(1) 5 hours; col(2) 24 min; col(3) 15 sec and col(4) ROTI! I did a test with six data in a 5 minutes period, and I found the average value (centered in the middle of the data), but how doI separate the average value of the others values, because I have files up to 20000 rows. Thanks!! – Marcelo Duarte Nov 16 '13 at 18:41
  • You're welcome. I am not sure what you are trying to achieve with the averaging thing. Could you give me an example? – heriantolim Nov 17 '13 at 00:34