copy file not working as it should
8 views (last 30 days)
Show older comments
hello there, i have a code that somehow just skips the copy file line and did not move file as it should. i tried adding [ status, message, messageId] to see whats going on and it did not return anything. it goes through the other lines fine, but it didnt copy it anywhere, much less the intended file. if someone can point out what i did wrong that will be very helpful. thanks!
mainfolder2 ='/Users/sesiliamaidelin/Downloads/summer project/Copy_of_CL2 Reviewer Network'; % Subfolders contains bitmap images
nufolderpath = '/Users/sesiliamaidelin/Downloads/summer project/nufolder'; % Define the path name of nufolder
filesAndFolders2 = dir([mainfolder2 '/**']); % Get all subfolder and files names
alllist2={filesAndFolders2.name}; % Convert to cell
bmpidx = cellfun(@(x) contains(x,'.bmp'),alllist2); % Index to extract dicom files only
bmplist = alllist2(bmpidx) % List of dicom files only
bmppath = {filesAndFolders2(bmpidx).folder}; % Extract the path of each dicom files
%%
for jj = 1 : numel(dcmlist)
[filepath, name, ext]= fileparts(dcmlist(jj));
ptt= digitsPattern + regexprep( dcmlist{jj},'.dcm$',' ')+'_';
for kk = 1 :numel(bmplist)
matchedIdx = contains(bmplist(kk), ptt)
if matchedIdx == true
movefile(fullfile(bmppath(kk),bmplist(kk)),fullfile( nufolderpath, name))
else
fprintf('file %s does not have a matching dicom file\n ', bmplist{kk});
end
end
end
2 Comments
Accepted Answer
Walter Roberson
on 16 Jul 2021
Edited: Walter Roberson
on 16 Jul 2021
for jj = 1 : numel(dcmlist)
we do not know what datatype dcmlist is yet
[filepath, name, ext]= fileparts(dcmlist(jj));
dcmlist is indexed with () indexing, and the result is something that it is expected fileparts() will work on. Taking into account that jj is a scalar index, the implication is that dcmlist(jj) is either a string scalar or a scalar cell of character vector
If dcmlist(jj) is s string scalar, then the outputs of fileparts will each be string scalars. If dcmlist(jj) is a cell array of character vector, then taking into account that jj is a scalar index, the outputs will be character vectors. If jj were a non-scalar index with dcmlist(jj) then being a non-scalar cell of character vectors, then the output would be cell arrays of character vectors.
ptt= digitsPattern + regexprep( dcmlist{jj},'.dcm$',' ')+'_';
We demonstrated above that dcmlist must be either a string array or a cell array of character vector. Either way, with jj being scalar, using {jj} indexing is going to give back a character vector. So you are passing a character vector into regexprep(), and the result will then be a character vector. The transformation you do in the regexprep() is to replace any character, followed by 'dcm' at the end of the string, with a single blank. The '.' in '.dcm' will match any character, including period, not just period specifically. A little sloppy but probably good enough.
The pattern you build into ptt will match at least one digit in a row, followed by the name of the file with '.dcm' changed to a blank, followed by underscore. The blank here needs to be emphasized: you definitely have ' ' as the parameter rather than '' so you are changing '.dcm' to blank, not just deleting the '.dcm'
matchedIdx = contains(bmplist(kk), ptt)
I really have to wonder whether bmplist() contains blanks.
alllist2={filesAndFolders2.name}; % Convert to cell
alllist2 is a cell array of character vectors
bmpidx = cellfun(@(x) contains(x,'.bmp'),alllist2); % Index to extract dicom files only
bmpidx is a logical vector
bmplist = alllist2(bmpidx) % List of dicom files only
logical vector, () indexing into a cell array of character vectors, so bmplist is a cell array of character vectors.
matchedIdx = contains(bmplist(kk), ptt)
contains() is happy to work on a scalar cell array of character vector, and will return a logical scalar.
movefile(fullfile(bmppath(kk),bmplist(kk)),fullfile( nufolderpath, name))
We established that bmplist is a cell array of character vectors. bmppath() is as well, and () indexing into a cell array of character vectors returns a cell array of character vectors. What datatype does fullfile() return when passed in cell arrays of character vectors? Answer: it returns a cell array of character vectors.
Thus, if you got here at all (if that blank really is part of the name, which I doubt), then you would be executing movefile() with the first parameter being a cell array of character vector. However, the first parameter to movefile() must be a character vector or string scalar, never a cell array of character vectors, so movefile() would fail if the code got to here.
More Answers (1)
Jan
on 16 Jul 2021
Use the debugger to find out, what's going on:
for kk = 1 :numel(bmplist)
if contains(bmplist{kk}, ptt)
sourceFile = fullfile(bmppath{kk}, bmplist{kk});
destFile = fullfile(nufolderpath, name);
[succ, msg] = movefile(sourceFile, destFile);
if ~isfile(destFile)
error('File not copied?! %s -> %s', sourceFile, destFile);
end
else
fprintf('file %s does not have a matching dicom file\n', bmplist{kk});
end
end
Now set a breakpoint in the IF block and step through the code line by line. Is MOVEFILE really reached?
Such lines a fragile:
ptt= digitsPattern + regexprep( dcmlist{jj},'.dcm$',' ')+'_';
Adding CHARs lead to unexpected results, if the former argument is not a string:
'A' + '_'
% 160, no 'A_'
11 Comments
Jan
on 20 Jul 2021
Asking many question about Matlab is the purpose of this forum. So your are welcome.
It is still not clear, what "doesn't work" means. Please replace:
movefile(fullfile(bmppath{kk},bmplist{kk}),fullfile(nufolderpath,name,'reviewer network'))
by:
sourceFile = fullfile(bmppath{kk},bmplist{kk});
destFolder = fullfile(nufolderpath,name,'reviewer network');
[status, msg] = movefile(sourceFile, destFolder);
if status ~= 1
error('Cannot copy file %s to %s: %s', sourceFile, destFile, msg);
end
Maybe the destination folder is write protected? Under Windows this does not mean, that you cannot write into this folder... (I hate it). Then:
[status, msg] = movefile(sourceFile, destFolder, 'f');
Please run this and explain exactly, what you observe. I guess, that the copy works as wanted, but you expect it to appear on another location.
See Also
Categories
Find more on DICOM Format 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!