Find string inside a cell array

5 visualizzazioni (ultimi 30 giorni)
Iugo
Iugo il 28 Mar 2021
Commentato: Iugo il 28 Mar 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!

Risposta accettata

Stephen23
Stephen23 il 28 Mar 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 Commenti
Image Analyst
Image Analyst il 28 Mar 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 il 28 Mar 2021
Thank you so much @Image Analyst!!

Accedi per commentare.

Più risposte (1)

Image Analyst
Image Analyst il 28 Mar 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 Commento
Iugo
Iugo il 28 Mar 2021
Modificato: Iugo il 28 Mar 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?

Accedi per commentare.

Categorie

Scopri di più su Data Type Conversion in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by