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

24 次查看(过去 30 天)
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?

采纳的回答

Walter Roberson
Walter Roberson 2021-3-27
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by