Find string inside a cell array

5 Ansichten (letzte 30 Tage)
Iugo
Iugo am 28 Mär. 2021
Kommentiert: Iugo am 28 Mär. 2021
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!

Akzeptierte Antwort

Stephen23
Stephen23 am 28 Mär. 2021
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 Kommentare
Image Analyst
Image Analyst am 28 Mär. 2021
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 am 28 Mär. 2021
Thank you so much @Image Analyst!!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
Image Analyst am 28 Mär. 2021
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 Kommentar
Iugo
Iugo am 28 Mär. 2021
Bearbeitet: Iugo am 28 Mär. 2021
@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?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by