Find string inside a cell array
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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!
0 Kommentare
Akzeptierte Antwort
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
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.
Weitere Antworten (1)
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
Siehe auch
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!