How to convert decimal hour and calculate the average every 5 min?

Hello all! 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).
Example:
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

2 Comments

Why are you dropping the 0.004 in the first step?
>> [Y, M, D, H, MN, S] = datevec(5.404/24)
Y =
0
M =
0
D =
0
H =
5
MN =
24
S =
14.4000
Thank you very much Doug! Now, how can I show this result at 5:24 format at UT column together with the ROT column?
Thanks!

Sign in to comment.

 Accepted Answer

sprintf('%02d:%02d', H, MN)
In order to show this "together" with the ROT, you will need to indicate the mechanism you wish to use to "show" it. Are you referring to a uitable() for example?
Ordinary numeric matrices cannot hold a combination of strings and numbers.

3 Comments

Thanks Walter Roberson, I used textscan command to read dat file and stored as ut=data{1} and rot=data {2}.I need to calculate every 5 minutes about this equation:
ROTI=sqrt(<rot^2>-<rot>^2)
then I did
  1. rot_quad=rot.^2;
  2. rot_quad_mean=mean(rot_quad);
  3. rot_mean=mean(rot)
  4. rot_mean_quad=rot_mean^2
  5. ROTI=sqrt((rot_quad_mean-rot_mean_quad)). %e.g: 5:24 to 5:29= step 1 to 4
and finally ROTI. Thanks in advance!
bin_num = 1 + floor(ut(:) * 60 / 5) - floor(ut(1) * 60 / 5);
max_bin_num = max(bin_num);
rot_quad = rot.^2;
rot_quad_mean5 = accumarray(bin_num, rot_quad(:), [], @mean);
rot_mean5 = accumarray(bin_num, rot(:), [], @mean);
rot_mean_quad5 = rot_mean5 .^ 2;
ROTI_5 = sqrt(rot_quad_mean5 - rot_mean_quad5);
first_date_vec = datevec(ut(1) * 24);
first_date_hour = first_date_vec(4);
first_date_min5 = floor(first_date_vec(5) ./ 5) .* 5;
first_date_ser = datenum([0 0 0 first_date_hour first_date_min5 0]);
bin_date_vecs = datevec(addtodate(first_date_ser, 5 * (0:max_bin_num-1), 'minute'));
ROTI_matrix = [bin_date_vecs(:,4), bin_date_vecs(:,5), ROTI_5];
Now, ROTI_matrix will be an array, first column the hour of each 5 minute interval, second column the minute of each 5 minute interval, third column the corresponding ROTI for the 5 minute interval.
Hi Walter! When I try to run the code, appears an error at addtodate (D,Q,F) command: addtodate (line 49) Quantity must be a numeric scalar. Then, I added the "norm" command in the Q field for convert from vector into scalar number:
bin_date_vecs = datevec(addtodate(first_date_ser, norm( 5 * (0:max_bin_num-1)), 'minute'));
Is it correct do this change?
After the change, I tried to run the code again but, appeared other error: Error using horzcat Dimensions of matrices being concatenated are not consistent.
Error in roti2 (line 40) ROTI_matrix= [bin_date_vecs(:,4), bin_date_vecs(:,5), ROTI_5];
How can I resolve the error horzcat?
Thanks in advance!

Sign in to comment.

More Answers (1)

k = rem(UT,1)*60;
tms = fix([UT,k]);
m = ceil(k/5)*5;
[~,b,ii] = unique(m);
out = [tms(b,:), accumarray(ii,ROT,[],@mean)];

1 Comment

Thanks Andrei! The program generated a 'b' array 13x1 and another 'out' array 13x3. I used a file with 5 hours of data, so 'b' and 'out' arrays needed to have 61 average values. Can you help me solve it? Thanks in advance.

Sign in to comment.

Tags

No tags entered yet.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!