Effacer les filtres
Effacer les filtres

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

24 vues (au cours des 30 derniers jours)
azarang asadi
azarang asadi le 27 Mar 2021
Commenté : azarang asadi le 27 Mar 2021
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?

Réponse acceptée

Walter Roberson
Walter Roberson le 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

Plus de réponses (0)

Catégories

En savoir plus sur File Operations dans Help Center et File Exchange

Tags

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by