필터 지우기
필터 지우기

Using contains() with dir() to search files in folder

조회 수: 80 (최근 30일)
LabRat
LabRat 2022년 7월 29일
편집: Stephen23 2022년 7월 30일
%{
trying to figure out how to search a folder for a file that contains a
matching string
serials is a 1x26 cell
%}
files_in_folder = dir('folder');
TF = contains(files_in_folder.name, serials);
%{
Error using contains
Incorrect number of arguments. Each parameter name must be followed by
a corresponding value.
%}
TF = contains(files_in_folder.name, serials(1))
%{
Error using contains
Incorrect number of arguments. Each parameter name must be followed by a
corresponding value.
%}
for i = files_in_folder
if TF == 1
File = strcat('example.txt');
end
end
%{
trying a few different variations:
%}
for i = files_in_folder
if contains(files_in_folder, serials) == 1
disp(serials);
end
end
for i = files_in_folder.name
if contains(files_in_folder.name, serials) == 1
disp(short_serials);
end
end
%{
A dot '.' indexing expression produced a comma-separated list with 15
values where only a single value is allowed.
%}

답변 (1개)

Stephen23
Stephen23 2022년 7월 29일
편집: Stephen23 2022년 7월 29일
Replace this comma-separated list
files_in_folder.name
with this cell array of filenames:
{files_in_folder.name}
S = dir(..);
TF = contains({S.name}, serials);
  댓글 수: 2
LabRat
LabRat 2022년 7월 29일
Thanks!
if I do something like this:
TF = contains({files_in_folder.name}, short_serials(2));
the result is:
1 0 0 0 0 0 0 0 0 0
Once I get an index with a value of 1, I want to load a file. In this example, {files_in_folder(1).name} = 1, so I want to load that file name in {files_in_folder(1).name}.
I tried a for loop but get the following error:
TF = contains(files_in_folder.name, app.SelectTestID.Value);
counter = 0;
for i = files_in_folder
counter = counter + 1;
if TF(counter) == 1
VideoFile = files_in_folder(counter).name;
end
end
Error using contains
Incorrect number of arguments. Each parameter name must be followed by
a corresponding value.
If I instead use:
TF = contains({files_in_folder.name}, app.SelectTestID.Value);
Then I get this error:
Unrecognized function or variable 'VideoFile'.
at the following line of code:
vidObj = VideoReader(VideoFile);
Stephen23
Stephen23 2022년 7월 30일
편집: Stephen23 2022년 7월 30일
Rather than using a loop, you should simply use that index directly. For example:
TF = contains({files_in_folder.name}, app.SelectTestID.Value);
assert(nnz(TZ)==1,'Exactly one filename must match')
VideoFile = files_in_folder(TF).name;

댓글을 달려면 로그인하십시오.

카테고리

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