Akby Akby - 12 days ago 6
C Question

GMT subtraction on MATLAB

I'm currently working on a small project on handling time difference on MATLAB. I have two input files;

Time_in and Time_out
. The two files contain arrays of time in the format e.g
2315 (GMT - Hours and Minute)


I've read both
Time_in' and 'Time_out
on MATLAB but I don't know how to perform the subtraction. Also, I want the corresponding answers to be in minutes domain only e.g (2hrs 30mins = 150minutes)

Answer

this is one of several possible solutions:

First, you should convert your time strings to a MATLAB serial date number. If you've done this, you can do your calculation as you want:

% input time as string
time_in = '2115';
time_out = '2345';

% read the input time as datenum
dTime_in = datenum(time_in,'HHMM');
dTime_out = datenum(time_out,'HHMM');

% subtract to get the time difference
timeDiff = abs(dTime_out - dTime_in);

% Get the minutes of the time difference
timeout = timeDiff * 24 * 60;

Furthermore, to calculate the time differences correctly you also should put some information about the date in your time vector, in order to calculate the correct time around midnight.

If you need further information about the function datenum you should read the following part of the MATLAB documentation:

https://de.mathworks.com/help/matlab/ref/datenum.html

Any questions?