Read custom time format from txt or xlsx file

Hi
I'm having some trouble with the time stamp from a dataset.
The data is pulled from LabVIEW and gives the following timestamp to each reading.
it represents HHmmss,sss but i can't find a way to convert it into HH:mm:ss,sss.
Best Regards

 Réponse acceptée

As long as you process each line as a char array it is as easy as simply inserting the colons:
%read your file to cellstr with my readfile function, or to a string vector
%with readlines (R2020b or later)
data={'Time';'083550,422';'083550,923';'083551,424'};
time_list=data(2:end);
for n=1:numel(time_list)
tmp=time_list{n};
tmp=[tmp(1:2),':',tmp(3:4),':',tmp(5:end)];
time_list{n}=tmp;
end
time_list
time_list = 3×1 cell array
{'08:35:50,422'} {'08:35:50,923'} {'08:35:51,424'}

6 commentaires

Yeah, but i can't seem to get it to read as a char array.
I have Matlab R2021a but the readlines function can't read my txt or xlsx file.
Is there a way to read it as a number?
Rik
Rik le 17 Mai 2021
Please attach the txt file to your question. That way I can test with your file specifically.
Also, if you are encountering any errors, you should describe them. The only response to 'it doesn't work' is 'then try something else'.
Ofcourse.
Here it is.
I don't see any issues.
data=readlines(...
'https://www.mathworks.com/matlabcentral/answers/uploaded_files/620253/PT1000.txt',...
'EmptyLineRule','skip');
%the next update to readfile will also include this switch
%(https://www.mathworks.com/matlabcentral/fileexchange/68780-readfile)
data=cellstr(data);%cast string to cellstr to make indexing easier
for n=1:numel(data)
tmp=data{n};
tmp=[tmp(1:2),':',tmp(3:4),':',tmp(5:end)];
data{n}=tmp;
end
data
data = 49136×1 cell array
{'08:35:50,422→22,034444E+0→21,876206E+0→19,941067E+0'} {'08:35:50,923→22,034444E+0→21,876206E+0→19,941067E+0'} {'08:35:51,424→22,034444E+0→21,876206E+0→19,941067E+0'} {'08:35:51,924→22,034444E+0→21,876206E+0→19,941067E+0'} {'08:35:52,425→22,034444E+0→21,876206E+0→19,941067E+0'} {'08:35:53,145→22,034768E+0→21,880902E+0→19,913695E+0'} {'08:35:53,426→22,034768E+0→21,880902E+0→19,913695E+0'} {'08:35:53,984→22,034768E+0→21,880902E+0→19,913695E+0'} {'08:35:54,428→22,034768E+0→21,880902E+0→19,913695E+0'} {'08:35:55,099→22,034444E+0→21,884790E+0→19,892478E+0'} {'08:35:55,428→22,034444E+0→21,884790E+0→19,892478E+0'} {'08:35:55,928→22,034444E+0→21,884790E+0→19,892478E+0'} {'08:35:56,428→22,034444E+0→21,884790E+0→19,892478E+0'} {'08:35:56,928→22,033310E+0→21,889001E+0→19,875310E+0'} {'08:35:57,428→22,033310E+0→21,889001E+0→19,875310E+0'} {'08:35:57,930→22,033310E+0→21,889001E+0→19,875310E+0'} {'08:35:58,431→22,033310E+0→21,889001E+0→19,875310E+0'} {'08:35:58,930→22,033310E+0→21,893860E+0→19,862353E+0'} {'08:35:59,431→22,033310E+0→21,893860E+0→19,862353E+0'} {'08:35:59,932→22,033310E+0→21,893860E+0→19,862353E+0'} {'08:36:00,433→22,033310E+0→21,893860E+0→19,862353E+0'} {'08:36:00,933→22,034606E+0→21,899690E+0→19,851663E+0'} {'08:36:01,433→22,034606E+0→21,899690E+0→19,851663E+0'} {'08:36:01,935→22,034606E+0→21,899690E+0→19,851663E+0'} {'08:36:02,435→22,034606E+0→21,899690E+0→19,851663E+0'} {'08:36:03,029→22,033148E+0→21,905683E+0→19,839354E+0'} {'08:36:03,435→22,033148E+0→21,905683E+0→19,839354E+0'} {'08:36:04,144→22,033148E+0→21,905683E+0→19,839354E+0'} {'08:36:04,479→22,033148E+0→21,905683E+0→19,839354E+0'} {'08:36:04,936→22,032824E+0→21,911999E+0→19,828827E+0'}
(the tabs are shown as arrows here)
It works.
Thank you.
Rik
Rik le 17 Mai 2021
You're welcome. If my answer solved your issue, please consider marking it as accepted answer. If not feel free to comment with your remaining issues.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by