How to get only duration calculated from datetime type variables as an integer/real scalar?
    22 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    lexi11
      
 el 6 de Dic. de 2016
  
    
    
    
    
    Comentada: Steven Lord
    
      
 el 15 de Feb. de 2023
            Hi,
I have some data from a sensor with date-time values and I need to compute something like below:
timeduration = max(datetimevalue) - min(datetimevalue);
rate = A/timeduration ;%A is a set of double values; sensor readings within the duration
where, max(datetimevalue) = 02-Dec-2016 15:30:26 and min(datetimevalue) = 02-Dec-2016 15:30:06
So I get timeduration as 00:00:20.
when I use rate = A/timeduration, it shows an error 'The denominator in matrix division for duration arrays must be a real scalar double value'.
What I want to do is, let matlab calculate the duration and take the duration as an integer (in the above case it is 20) and calculate the rate.
How do I do this? For an experiment or two, I can manually change it, but when I have many experiments to do, I would like to make matlab do it automatically so I do not have to hard-code it. (I have tried using etime, but no good).
Thanks in advance.
0 comentarios
Respuesta aceptada
  Steven Lord
    
      
 el 6 de Dic. de 2016
        What do you want that scalar to be? How many years, days, hours, minutes, seconds, or milliseconds that duration represents? There are functions for each of those. In this context you're probably looking for seconds. If S = seconds(X) and "X is a duration array, then S is a double array with each element equal to the number of seconds in the corresponding element of X."
Más respuestas (2)
  Geoff Hayes
      
      
 el 6 de Dic. de 2016
        laksi11 - one way would be for you to convert your datetime values into serial numbers using datenum as
 timeDurationSerial = datenum(max(datetimevalue)) - datenum(min(datetimevalue));
and then convert that duration into seconds as
 timeDurationSec = timeDurationSerial*86400;
The timeDurationSerial is a serial number which represents the whole and fractional number of days from a fixed, preset date. So we convert this difference to seconds using 86400 (the number of seconds in a day). Your code could then be simply
 timeDurationSerial = (datenum(max(datetimevalue)) - datenum(min(datetimevalue)))*86400;
The above should work...I can't test it out since R2014a doesn't have the datetime function.
2 comentarios
  Sean de Wolski
      
      
 el 6 de Dic. de 2016
				I would not recommend this. Datetime has many features lacking from datenum, use the conversion functions like seconds, hours, etc. to get the double output.
  Mohd Owais
 el 15 de Feb. de 2023
        % Usain Bolt's 100-meter dash record
distance = 100; % meters
time = 9.58; % seconds
hundred = (distance/time) * 3.6; % km/h
% Eliud Kipchoge's marathon record
distance = 42.195; % kilometers
time = hours(2) + minutes(1) + seconds(39); % convert to duration
marathon = (distance/time) * 3.6; % km/h
3 comentarios
  Steven Lord
    
      
 el 15 de Feb. de 2023
				Convert your second time variable into a number of hours using the hours function.
time = hours(2) + minutes(1) + seconds(39)
timeInHours = hours(time)
whos
Note that time is a duration array while timeInHours is a double value.
Ver también
Categorías
				Más información sobre Dates and Time en Help Center y File Exchange.
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!





