필터 지우기
필터 지우기

Find string inside a cell array

조회 수: 1 (최근 30일)
Iugo
Iugo 2021년 3월 28일
댓글: Iugo 2021년 3월 28일
Hi everyone!
I have a variable called filenames (cell array), which will store a random number of files, and PasteInfo2 (cell array), which stores all the files. I want to store the name of the file that matches between in thisfile and PasteInfo2 into the variable name. I already have this portion of code but still can't store the name of that file...
How can I do that?
PasteInfo2 = dir('D:\ABIDEdataset\Outputs\dparsf\nofilt_noglobal\rois_aal\All_Groups');
PasteInfo2_names = extractfield(PasteInfo2,'name');
NumberSubjs = length(filenames):
for i=1:NumberSubjs
thisfile = filenames{i};
if contains(thisfile,PasteInfo2_names)
name = %name of file that is suppposed to be in thisfile;
end
end
Thanks in advance!

채택된 답변

Stephen23
Stephen23 2021년 3월 28일
Skip the loop and use ismember:
S = dir('D:\ABIDEdataset\Outputs\dparsf\nofilt_noglobal\rois_aal\All_Groups');
C = extractfield(S,'name');
X = ismember(filenames,C);
out = filenames(X)
  댓글 수: 3
Image Analyst
Image Analyst 2021년 3월 28일
Try this:
[ia1, ib1] = ismember(filenames,C);
[ia2, ib2] = ismember(C, filenames);
Take whichever variable (ia1, ib1, ia2, ib2) gives you what you want.
Iugo
Iugo 2021년 3월 28일
Thank you so much @Image Analyst!!

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

추가 답변 (1개)

Image Analyst
Image Analyst 2021년 3월 28일
Do you want to get the filenames something like this:
folder = 'D:\ABIDEdataset\Outputs\dparsf\nofilt_noglobal\rois_aal\All_Groups';
filePattern = fullfile(folder, '*.*');
% Get a list of all files in the folder.
PasteInfo2 = dir(filePattern)
% Extract just the filenames into its own cell array.
PasteInfo2_names = {PasteInfo2.name}
numberOfSubjects = length(PasteInfo2)
% Loop over all files.
for k = 1 : numberOfSubjects
thisFullFileName = fullfile(folder, PasteInfo2_names{k});
fprintf('Reading %s...\n', thisFullFileName);
% if contains(thisfile,PasteInfo2_names)
% name = %name of file that is suppposed to be in thisfile;
% end
end
  댓글 수: 1
Iugo
Iugo 2021년 3월 28일
편집: Iugo 2021년 3월 28일
@Image Analyst thanks for your reply!
I think that what you wrote its not comparing if any file of PasteInfo2 (which stores all the names of the files) matches with the file in position i of filenames (which have a random portion of files from all the files that are also present in PasteInfo2)... or is something missing me?

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

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by