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

15 visualizzazioni (ultimi 30 giorni)
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?

Risposta accettata

Walter Roberson
Walter Roberson il 27 Mar 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

Più risposte (0)

Categorie

Scopri di più su File Operations in Help Center e File Exchange

Tag

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by