Ваша индексация и преобразование типов немного сбивают с толку, смотрите мои комментарии ...
% Your code:
timestamp=[];
for i=1:length(data) % Your loop variable is "i"
% You override the entire "timestamp" array here, with element "j" not "i" of "data"
% You also don't need to use getfield here
timestamp = getfield(data,{j},'timestamp');
% You override element "j" (again, not "i") with the CURRENT datetime.
% This line doesn't do any type conversion, "datetime" with no arguments is now!
% Also you're using curly braces for indexing, which isn't right for a datetime array
timestamp{j}=(datetime);
end
Вы можете исправить это следующим образом:
timestamp = NaT(1,length(data)); % pre-allocate the output to "not a time" array
for ii = 1:length(data)
t = getfield( data, {ii}, 'timestamp' );
t( t == 'T' | t == 'Z' ) = []; % remove T and Z, they will break datetime
timestamp( ii ) = datetime( t, 'InputFormat', 'yyyy-MM-ddHH:mm:ss.SSS' );
end
Вывод:
timestamp =
1×2 datetime array
03-Jun-2016 13:37:20 03-Jun-2016 13:37:21
(Создано с использованием вашего примера строки и той же строки с добавлением одной секунды).
Вот как исправить ваш код, вот как я бы это сделал:
timestamp = regexprep( {data.timestamp}, '[TZ]', '' );
timestamp = datetime( timestamp, 'InputFormat', 'yyyy-MM-ddHH:mm:ss.SSS' );