Comparing cell arrays and creation of subfolders
1 view (last 30 days)
Show older comments
Dear Matlab Community,
I have a text file ("X") containing the name of my samples in one of the columns (column "samples") and, on the other hand, a folder with a large number of text files with thousands of different names. I would like to select the files on that folder by comparing their names with the names on my text file.
So far, this is what I have...But It is (obviously) not working, and I don't know how to proceed to select the files I'm interested in and put them into a new subfolder.
direct = uigetdir('/Users/xxx/Documents/MATLAB/')
cd(direct)
readtable 'X.txt' %this is my text file, which I converted into a table
X = ans
X_names = {X.samples} %I've converted the "samples" column into a cell array (for the comparison)
d_names = dir('*.txt') %then I got all the text files from my folder as an structured array
%I wanted to compare the names of my X file (X_names) with the names of the text files on my folder...
%and save the "matching" files into the variable named "real_samples". After that, I need to select these text files...
% and put them into a new folder (remaining on the same directory). What can I do?
if isequal(d_names.name{k}, ind_vit.names{i})
real_samples =(d_names{k});
else
unclassified =(d_names{k});
end
0 Comments
Answers (1)
Stephen23
on 23 Dec 2020
A much more robust approach using absolute filenames (rather than buggy and slow CD) and function syntax rather than outdated command syntax:
P = uigetdir('/Users/xxx/Documents/MATLAB/');
X = readtable(fullfile(P,'X.txt'));
A = lower({X.samples});
S = dir(fullfile(P,'*.txt'));
B = lower({S.name});
Y = ismember(B,A);
R = B(Y); % real
U = B(~Y); % unclassified
I do not understand what "I need to select these text files and put them into a new folder (remaining on the same directory)" means, but perhaps MOVEFILE does what you want (you will probably need a loop).
0 Comments
See Also
Categories
Find more on Text Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!