How do I extract multiple numbers from a string or character vector?

I have a list of mixed text and numerical information that was generated by a third party software as an Excel file... example shown here:
7 mins 12 secs
12 mins 54 secs
1 min 1 sec
3 mins 5 secs
I want to calculate a single number of seconds for the total time represented by this information for each instance (so, '7mins 12 secs' would become 432.)
I have imported the original data into Matlab as a cell aray of character vectors, converted the cell array to a character array (time_taken_pre_char), and tried using the sscanf as
sscanf(time_taken_pre_char(1),'%d mins %d secs')
ans =
7
However, this doesn't work for the 'secs' numbers and I want to extract all the numbers into a cell array for simple multiplication; generating something like
[7, 12; 12, 54; 1, 1; 3, 5]
Any help is greatly appreciated!

 采纳的回答

Try this:
time_taken_pre_char = {'7 mins 12 secs'
'12 mins 54 secs'
'1 min 1 sec'
'3 mins 5 secs'};
for k = 1:size(time_taken_pre_char,1)
v(k,:) = sscanf(time_taken_pre_char{k},'%d mins %d secs');
end
secv = v*[60; 1]
producing:
secv =
432
774
61
185
Experiment to get the result you want.

更多回答(0 个)

类别

在 帮助中心 和 File Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by