Time format conversion command

This post is related to this thread:
If I want to convert
36:40.0
to
time in seconds, how do I do this?

2 Kommentare

Matt Fig
Matt Fig am 16 Okt. 2012
What form is that? Is it a string and many in a character array or a cell array or what?
A = ['36:40.0';'36:41.0';'34:40.3']; % Like this?
A = {'36:40.0';'36:41.0';'34:40.3'}; % Like this?
Azzi Abdelmalek
Azzi Abdelmalek am 16 Okt. 2012
are your data in a text file?

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 16 Okt. 2012
Bearbeitet: Azzi Abdelmalek am 16 Okt. 2012

0 Stimmen

t={'36:40.0' ,'37:40.0' ;'39:40.0' ,'31:40.0'}
out=cellfun(@(x) sum(cellfun(@str2double, regexp(x,'[:.]','split')).*[3600 60 1]),t)

7 Kommentare

Azzi Abdelmalek
Azzi Abdelmalek am 16 Okt. 2012
look at edited answer
Azzi Abdelmalek
Azzi Abdelmalek am 17 Okt. 2012
Bearbeitet: Azzi Abdelmalek am 17 Okt. 2012
post a sample of data
data{2} is not a cell array while t is
Azzi Abdelmalek
Azzi Abdelmalek am 17 Okt. 2012
Because you've added a 1/100 sec. I have to change [3600 60 1]
Azzi Abdelmalek
Azzi Abdelmalek am 17 Okt. 2012
Bearbeitet: Azzi Abdelmalek am 17 Okt. 2012
try
out=cellfun(@(x) sum(cellfun(@str2double, regexp(x,'[:.]','split')).*[3600 60 1 1/100]),data{2})
Azzi Abdelmalek
Azzi Abdelmalek am 17 Okt. 2012
Different does'nt say which one is correct
Azzi Abdelmalek
Azzi Abdelmalek am 18 Okt. 2012
Bearbeitet: Azzi Abdelmalek am 18 Okt. 2012
Anthony, I suggest that you reformulate and repost your question, let it brief and very clear.
T
T am 18 Okt. 2012
Actually I used the following:
[Y, M, D, H, MN, S] = datevec(data{2}); out = H*3600+MN*60+S;
It works!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Matt Fig
Matt Fig am 16 Okt. 2012
Bearbeitet: Matt Fig am 16 Okt. 2012

0 Stimmen

If you have a cell array, I would do this:
A = {'36:40.0';'36:40.1';'34:40.3'}; % A cell array
B = '${num2str(str2num($1)*60+str2num($2))}';
B = regexprep(A,'(\d+):(\d+\.\d*)',B)
If you have a character array, then:
A = ['36:40.0';'36:41.0';'34:40.3']; % A character array.
B = '${num2str(str2num($1)*60+str2num($2))}';
B = char(regexprep(cellstr(A),'(\d+):(\d+\.\d*)',B))

3 Kommentare

Matt Fig
Matt Fig am 16 Okt. 2012
Bearbeitet: Matt Fig am 16 Okt. 2012
I do not understand what you mean, "not CSV file has a time column like this."
If you read the file into a cell array or a character array, the corresponding code I gave you will convert it regardless of the range as long as it is MM:SS.F format (minutes:seconds.fraction)...
For example:
A = {'36:40.0','36:40.1','36:40.2','40:10.3','43:50.1'};
Now run the code I showed above in the first part.
B = '${num2str(str2num($1)*60+str2num($2))}';
B = regexprep(A,'(\d+):(\d+\.\d*)',B)
B =
'2200' '2200.1' '2200.2' '2410.3' '2630.1'
Matt Fig
Matt Fig am 16 Okt. 2012
Bearbeitet: Matt Fig am 16 Okt. 2012
You seem to show where you converted to datenumbers using the DATENUM command. So why would it be surprising that you get datenumbers?
Show what this shows:
data{2}(1:3) % Or, what is in data{2}... strings?
If you don't see the strings in there, take the time to explore the data cell array before you run all these conversions on it. What is in data{1}? How about data{3}, etc...
Matt Fig
Matt Fig am 16 Okt. 2012
Bearbeitet: Matt Fig am 16 Okt. 2012
Please show the first few elements of data{2}. Do so in another comment, don't go up and edit old comments or I cannot follow the conversation.

Melden Sie sich an, um zu kommentieren.

Kategorien

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by