필터 지우기
필터 지우기

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

조회 수: 24 (최근 30일)
azarang asadi
azarang asadi 2021년 3월 27일
댓글: azarang asadi 2021년 3월 27일
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