How can I extract an array of numbers from a text-formatted cell array of strings.
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
dormant
el 26 de Abr. de 2024
Respondida: dormant
el 14 de Mayo de 2024
I have read some data from a spreadsheet. The values are times, but formatted with s, m or h as a suffix to indicate seconds, minutes or hours. see example below,
I'd like to convert all of them to seconds, with NaN for the blank ones.
Is there an elegant way to do this?
Like this:
{'20s' }
{'15m' }
{0×0 char }
{'24s' }
{0×0 char }
{'44s' }
{'3h' }
{'40m' }
{'20s' }
{0×0 char }
{'14s' }
0 comentarios
Respuesta aceptada
Más respuestas (2)
Voss
el 26 de Abr. de 2024
data = {
'20s'
'15m'
''
'24s'
''
'44s'
'3h'
'40m'
'20s'
''
'14s'
};
C = regexp(data,'(\d+)([hms])','tokens','once');
idx = ~cellfun(@isempty,C);
C = vertcat(C{idx});
val = str2double(C(:,1));
[~,unit] = ismember(C(:,2),{'s','m','h'});
result = NaN(size(data));
result(idx) = val.*60.^(unit-1);
disp(result)
0 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!