MATLAB Answers

Convert cell array to vector

33 views (last 30 days)
M.Many
M.Many on 20 Jan 2021
Commented: M.Many on 20 Jan 2021
Hello everybody,
I have a 1279x1 cell array containing timestamps and I want to convert it as a 1279x1 vector of interpretable time format.
My ultimate goal would be to get a vector with the time differences between 2 successive timestamps and get the mean of this vector.
Then I can calculate the sample rate.
I have absolutely no idea how to deal with cell arrays, so any help would be really nice !
You can see my cell array in the following and the complete cell array as a .mat file :
{["16:44:53.419"]}
{["16:44:53.435"]}
{["16:44:53.468"]}
{["16:44:53.483"]}
{["16:44:53.499"]}
{["16:44:53.515"]}
{["16:44:53.531"]}
{["16:44:53.563"]}
{["16:44:53.579"]}
{["16:44:53.595"]}
{["16:44:53.611"]}
{["16:44:53.627"]}
{["16:44:53.643"]}
{["16:44:53.675"]}
{["16:44:53.691"]}
{["16:44:53.707"]}
{["16:44:53.723"]}
{["16:44:53.739"]}
{["16:44:53.755"]}
{["16:44:53.770"]}
{["16:44:53.787"]}
{["16:44:53.803"]}
{["16:44:53.818"]}
{["16:44:53.835"]}
{["16:44:53.962"]}
{["16:44:53.978"]}
{["16:44:53.994"]}
{["16:44:54.010"]}
{["16:44:54.027"]}
{["16:44:54.042"]}
{["16:44:54.074"]}
{["16:44:54.090"]}
{["16:44:54.106"]}
{["16:44:54.122"]}
{["16:44:54.138"]}
{["16:44:54.154"]}
{["16:44:54.170"]}
{["16:44:54.186"]}
{["16:44:54.202"]}
{["16:44:54.218"]}
{["16:44:54.234"]}
{["16:44:54.266"]}
{["16:44:54.282"]}
{["16:44:54.298"]}
{["16:44:54.314"]}
{["16:44:54.330"]}

  0 Comments

Sign in to comment.

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 20 Jan 2021
S = load('timestamps.mat');
T = vertcat(S.ans{:})
T = 1249×1 string array
"16:44:28.637" "16:44:28.669" "16:44:28.685" "16:44:28.701" "16:44:28.717" "16:44:28.733" "16:44:28.765" "16:44:28.781" "16:44:28.797" "16:44:28.813" "16:44:28.829" "16:44:28.957" "16:44:28.988" "16:44:29.005" "16:44:29.021" "16:44:29.037" "16:44:29.068" "16:44:29.085" "16:44:29.101" "16:44:29.116" "16:44:29.132" "16:44:29.164" "16:44:29.181" "16:44:29.196" "16:44:29.212" "16:44:29.228" "16:44:29.260" "16:44:29.276" "16:44:29.292" "16:44:29.308"
M = seconds(mean(diff(duration(T,'InputFormat','hh:mm:ss.SSS'))))
M = 0.0206

  1 Comment

M.Many
M.Many on 20 Jan 2021
Thanks a lot !

Sign in to comment.

More Answers (1)

Cris LaPierre
Cris LaPierre on 20 Jan 2021
T = load('timestamps.mat');
% Convert to a string array
ts = string(T.ans);
% Convert strings to durations
ts = duration(ts,'InputFormat',"hh:mm:ss.SSS","Format","hh:mm:ss.SSS");
% compute the difference between each row
dt = diff(ts);
% calculate the mean
mdt = mean(dt)
mdt = duration
00:00:00.020

  1 Comment

M.Many
M.Many on 20 Jan 2021
Thanks for the detailed explanation !

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!