Convert milliseconds to hours and graph

I am trying to convert an array of milliseconds and its corresponding data. However, I want to do this in a matter of hours and minutes.

Millis = [60000 120000 180000 240000....]
Power = [ 12 14 12 13 14 ...]

      

I configured it to write data every minute, so 60,000 millis

(= 1 minimte). I am trying to plot the time on an axis x

and turn the power on y

. I would like the axis to x

be displayed in hours and minutes with each corresponding power information corresponding to the corresponding time.

I tried this

for i=2:length(Millis)
Conv2Min(i) = Millis(i) / 60000;
Time(i) = startTime + Conv2Min(i);
if (Time(i-1) > Time(i) + 60)
Time(i) + 100;
end

end
s = num2str(Time);

      

This is an attempt at turning milliseconds into hours starting at 08:00, and once 60 minutes have elapsed before 09:00, the problem is plotting this. I am getting a break between 08:59 and 09:00. I also cannot support 0=initial 0

.

+3


source to share


3 answers


In this scenario, it is preferable to work with datenum

values ​​and then use datetick

to set the label format of your labels plot

to 'HH:MM'

.

Suppose you start taking measurements at t_1 = [HH_1, MM_1]

and stop taking measurements at t_2 = [HH_2, MM_2]

.

A nice trick for creating an array of values datenum

is to use the following expression:

time_datenums = HH_1/24 + MM_1/1440 : 1/1440 : HH_2/24 + MM_2/1440;

      

Explanation:

We create a vector at regular intervals time_datenums = A:B:C

using colon (:) operator

where A

is the start datenum

value B

is the increment between the values datenum

and C

is the end value datenum

.

Since your measurements were taken every minute (60,000 milliseconds), the increment between the values datenum

should be 1 minute. Since a day has 24 hours, that's 1440 minutes per day, so use B = 1/1440

as an increment between vector elements to get a 1 minute increment.

For A

and, C

we just need to divide the number of hours by 24 and minutes by 1440 and sum them as follows:



  • A = HH_1/24 + MM_1/1440

  • C = HH_2/24 + MM_2/1440

So, for example, if t_1 = [08, 00]

, then A = 08/24 + 00/1440

. So simple.

Note that this procedure does not use a function at datenum

all, and yet it manages to create a valid array of datenum

values ​​that count only in terms of time datenum

, without having to worry about the date datenum

. You can find out about it here and here .


Going back to the original problem, let's take a look at the code:

time_millisec = 0:60000:9e6;             % Time array in milliseconds.
power = 10*rand(size(time_millisec));    % Random power data.

% Elapsed time in milliseconds.
elapsed_millisec = time_millisec(end) - time_millisec(1); 
% Integer part of elapsed hours.
elapsed_hours_int = fix(elapsed_millisec/(1000*60*60));
% Fractional part of elapsed hours.
elapsed_hours_frac = (elapsed_millisec/(1000*60*60)) - elapsed_hours_int;

t_1 = [08, 00]; % Start time 08:00
t_2 = [t_1(1) + elapsed_hours_int, t_1(2) + elapsed_hours_frac*60]; % Compute End time.

HH_1 = t_1(1); % Hour digits of t_1
MM_1 = t_1(2); % Minute digits of t_1

HH_2 = t_2(1); % Hour digits of t_2
MM_2 = t_2(2); % Minute digits of t_2

time_datenums = HH_1/24+MM_1/1440:1/1440:HH_2/24+MM_2/1440; % Array of datenums.

plot(time_datenums, power);    % Plot data.
datetick('x', 'HH:MM');        % Set 'HH:MM' datetick format for the x axis.

      

This is the conclusion:

plot with data format

+4


source


I would use the data:

Millis = [60000 120000 180000 240000 360000];
Power = [ 12 14 12 13 14 ];
d = [2017 05 01 08 00 00]; %starting point (y,m,d,h,m,s)
d = repmat(d,[length(Millis),1]);
d(:,6)=Millis/1000; %add them as seconds
D=datenum(d); %convert to datenums
plot(D,Power) %plot
datetick('x','HH:MM') %set the x-axis to datenums with HH:MM as format

      



an even shorter approach would be: (thanks to the idea code engine)

Millis = [60000 120000 180000 240000 360000];
Power = [ 12 14 12 13 14 ];
D = 8/24+Millis/86400000; %24h / day, 86400000ms / day
plot(D,Power) %plot
datetick('x','HH:MM') %set the x-axis to datenums with HH:MM as format

      

+1


source


I think there is an easier way to use datetick

and datenum

, but I couldn't figure it out. This should fix your problem at the moment:

Millis=6e4:6e4:6e6;
power=randi([5 15],1,numel(Millis));
hours=floor(Millis/(6e4*60))+8; minutes=mod(Millis,(6e4*60))/6e4; % Calculate the hours and minutes of your Millisecond vector.
plot(Millis,power)
xlabels=arrayfun(@(x,y) sprintf('%d:%d',x,y),hours,minutes,'UniformOutput',0); % Create time-strings of the format HH:MM for your XTickLabels
tickDist=10; % define how often you want your XTicks (e.g. 1 if you want the ticks every minute)
set(gca,'XTick',Millis(tickDist:tickDist:end),'XTickLabel',xlabels(tickDist:tickDist:end))

      

0


source







All Articles