필터 지우기
필터 지우기

stringcmp filenames with similar naming

조회 수: 1 (최근 30일)
LabRat
LabRat 2022년 7월 25일
댓글: LabRat 2022년 7월 29일
Hello:
Is there a good way to match files from different folders together?
I am considering using stringcmp, to load two different files together, but I am not sure how to approach this. Would you first use a for loop for recursion to search all folders and files one string character at a time? If so, what would be the matlab syntax?
  댓글 수: 2
Matt J
Matt J 2022년 7월 25일
편집: Matt J 2022년 7월 25일
There is no native Matlab function called stringcmp. Perhaps you meant strcmp
Do you mean you have a collection of folders and you want to see which ones contain identically named files? If not, please give an example of the input and desired output.
LabRat
LabRat 2022년 7월 25일
Hi Matt, yes I am looking to match files with a portion of the string matching from both files. I found the 'contains' and 'strfind' functions so far. Still working on how to use these correctly.

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

답변 (1개)

Voss
Voss 2022년 7월 26일
편집: Voss 2022년 7월 26일
"Is there a good way to match files from different folders together?"
Here's an approach using dir, fullfile, fileparts, strcat, and intersect that does what it seems like you are asking for:
% create some txt files in two directories:
rng(1000); % (for reproducibility of the random file names)
dirs = {'folder_1','folder_2/subfolder'};
for ii = 1:numel(dirs)
mkdir(dirs{ii});
for jj = randi(10,1,7)
fclose(fopen(fullfile(dirs{ii},sprintf('file_%03d.txt',jj)),'w'));
end
end
% get info about txt files in those two directories:
files_1 = dir(fullfile(dirs{1},'*.txt'));
names_1 = fullfile(dirs{1},{files_1.name}.')
names_1 = 7×1 cell array
{'folder_1/file_001.txt'} {'folder_1/file_002.txt'} {'folder_1/file_003.txt'} {'folder_1/file_005.txt'} {'folder_1/file_007.txt'} {'folder_1/file_009.txt'} {'folder_1/file_010.txt'}
files_2 = dir(fullfile(dirs{2},'*.txt'));
names_2 = fullfile(dirs{2},{files_2.name}.')
names_2 = 5×1 cell array
{'folder_2/subfolder/file_002.txt'} {'folder_2/subfolder/file_003.txt'} {'folder_2/subfolder/file_004.txt'} {'folder_2/subfolder/file_008.txt'} {'folder_2/subfolder/file_009.txt'}
% use fileparts to get just the names (with extensions):
[~,fn_1,ext_1] = fileparts(names_1);
fn_1 = strcat(fn_1,ext_1)
fn_1 = 7×1 cell array
{'file_001.txt'} {'file_002.txt'} {'file_003.txt'} {'file_005.txt'} {'file_007.txt'} {'file_009.txt'} {'file_010.txt'}
[~,fn_2,ext_2] = fileparts(names_2);
fn_2 = strcat(fn_2,ext_2)
fn_2 = 5×1 cell array
{'file_002.txt'} {'file_003.txt'} {'file_004.txt'} {'file_008.txt'} {'file_009.txt'}
% use intersect to find which file names are in both directories:
intersect(fn_1,fn_2)
ans = 3×1 cell array
{'file_002.txt'} {'file_003.txt'} {'file_009.txt'}
If this is not what you want to do, clarify what you want to do.
  댓글 수: 1
LabRat
LabRat 2022년 7월 29일
Hello, thanks for your repsonse. I tried to clarify here:
https://www.mathworks.com/matlabcentral/answers/1770495-using-contains-with-dir-to-search-files-in-folder?s_tid=srchtitle

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

카테고리

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