Borrar filtros
Borrar filtros

How to find the file in a folder that contains a specific word

24 visualizaciones (últimos 30 días)
I have three files in my folder:
subject10_post_acc_global.sto
subject10_post_vel_global.sto
subject10_post_pos_global.sto
I need to find the filename containing 'pos' in a variable. so what I want is this:
myFile = subject10_post_pos_global.sto
How do I get that?

Respuesta aceptada

Walter Roberson
Walter Roberson el 27 de Mzo. de 2021
projectdir = 'appropriate folder name'; %can be '.'
dinfo = dir(fullfile(projectdir, '*_pos_*.sto'));
if isempty(dinfo)
error('no pos file in directory "%s"', projectdir);
end
filename = dinfo(1).name;
Or if you already have a directory structure,
%assuming dinfo is an existing directory structure
filenames = {dinfo.name};
filename = filenames{contains(filenames, '_pos_')};
if isempty(filename)
error('no pos file here');
end

Más respuestas (0)

Categorías

Más información sobre File Operations 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