Extract part of a filename from a directory list

7 visualizaciones (últimos 30 días)
Tom Wright
Tom Wright el 14 de Ag. de 2012
Comentada: Sogand Karbalaieali el 12 de Abr. de 2017
Hi, Can anyone please show me a 'nice' way to achieve what I what, preferably without looping.
I have a folder containing multiple files with similar format names 'XXX_YYY_010203.txt'. The numeric part is a timestamp, I would like to extract the timestamps into an array.
Currently my code looks like this:
filelist=dir(folder);
x=regexp({filelist(:).name},'_','split')
x=cellfun(@(x) regexp(x(3),'\.','split'),x)
x=cellfun(@(x) x(1),x)
Which works, but isn't exactly readable.

Respuesta aceptada

Oleg Komarov
Oleg Komarov el 14 de Ag. de 2012
I suppose that your filenames always have the date as ddmmyy and it's always at then end:
x = {'XXX_YYY_010203.txt'
'XXX_YYY_020203.txt'};
x = cellfun(@(in) in(end-9:end-4),x,'un',0)
  2 comentarios
Tom Wright
Tom Wright el 14 de Ag. de 2012
Thanks
x = cellfun(@(in) in(end-9:end-4),{filelist(:).name},'un',0)
Sogand Karbalaieali
Sogand Karbalaieali el 12 de Abr. de 2017
Thank you. Also, if all file names do not have the same patterns, those with different name pattern shall be excluded to avoid error in the code.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Environment and Settings en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by