copying file to a folder makes a new folder instead

3 views (last 30 days)
hi, so i want my code to create a folder based on a list of file's names, but when i want to move the file to the folder i created, it made a new folder instead with the name of the variable i used. help please
I = the files
for gg = 1: numel(I)
bmp = {}; %the files I want to move
bmp_n = char(bmp(gg));
for ii = 1: numel(Plist)
P = dir( fullfile( mainfolder,Plist{ii}, '*.dcm'));
dcm = {P(~[P.isdir]).name};
for jj = 1 : numel(dcm)
dicom = char(dcm(jj)); % converts cell to string
mkdir( parentfolder, dicom); % (dicom is the variable of the name of the new folders, the new
%folders' name are eg 00004, 00005)
ptt = digitsPattern + regexprep(dicom, '.dcm$', '') + "_";
matchedIdx = contains(bmp_n, ptt) % matching the name of the files to the name of the folder
if matchedIdx == true
copyfile input dicom % in this part it makes a new file dicom and move the files there instead of moving it to 00004, 00005, 00006 etc)
any help would be greatly appreciated!
  1 Comment
dpb on 14 Jul 2021
copyfile input dicom %
is the command line form for copyfile -- "input" and "dicom" are interpreted as literal strings, not as variables. Also input is the builtin MATLAB function and should not be used as variable -- and it isn't defined in the above code otherwise.
where the first argument needs to be a variable that contains the matching file names in the matched pattern. You'll need to iterate over the number of elements that are TRUE in matchedIdx as copyfile isn't vectorized I do not believe.

Sign in to comment.

Accepted Answer

Simon Chan
Simon Chan on 14 Jul 2021
Note that "myFolder" is the current folder storing the DCM files and the destination folders.
If they are in two different paths, then you may need to change the path:
nfiles = dir(myFolder);
folders = {nfiles([nfiles.isdir]).name};
files = dir(fullfile(myFolder,'*.dcm'));
for i = 1:length(filename)
matchedIdx = find(cellfun(@(x) contains(filename{i},x),folders));
copyfile(filename{i}, fullfile(myFolder,'\',folders{matchedIdx}));

Sign in to comment.

More Answers (0)




Community Treasure Hunt

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

Start Hunting!

Translated by