Find string inside a cell array

1 visualización (últimos 30 días)
Iugo
Iugo el 28 de Mzo. de 2021
Comentada: Iugo el 28 de Mzo. de 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!

Respuesta aceptada

Stephen23
Stephen23 el 28 de Mzo. de 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 comentarios
Image Analyst
Image Analyst el 28 de Mzo. de 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 el 28 de Mzo. de 2021
Thank you so much @Image Analyst!!

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 28 de Mzo. de 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 comentario
Iugo
Iugo el 28 de Mzo. de 2021
Editada: Iugo el 28 de Mzo. de 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?

Iniciar sesión para comentar.

Categorías

Más información sobre Data Type Identification en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by