How to solve this batch process error: "Error using movefile The process cannot access the file because it is being used by another process" ?

78 views (last 30 days)
I'm trying to run this code to batch process some image stacks. I get an error code in the end, when I try to rename each 'test.tif' file I get at the end of the second for loop using a movefile function. I want to rename them as test_1.tif, test_2.tif as they are being processed within the first for loop. When I run the code using the Image-BatchProcess App, I get an error message "Error using movefile The process cannot access the file because it is being used by another process". I am not able to figure out which process is still using the 'test.tif' file that I generate at the end of the second for loop. I'd appreciate any thoughts/help here.
Here is my code:
Image_folder= uigetdir;
Diff_stack_folder=uigetdir;
Image_Dir=dir([Image_folder '/*.tif']);
total_images=size(Image_Dir, 1);
for n= 1: total_images
filename= Image_Dir.name;
fid=fopen(filename);
info = imfinfo(filename);
num_images = numel(info);
for k = 1:num_images - 1
A = imread(filename, k, 'Info', info);
B = imread(filename, k + 1, 'Info', info);
Q = A - B;
imshow(Q, []);
imwrite(Q,'test.tif','WriteMode', 'append')
end
[~, f,ext] = fileparts(Image_Dir(n).name);
rename = sprintf('test_%d.tif',n);
movefile(Image_Dir(n).name, rename);
end

Accepted Answer

Fangjun Jiang
Fangjun Jiang on 5 Feb 2019
You have the file open with fid=fopen(filename)
run fclose() before you run movefile().
  4 Comments
Anna Paulson
Anna Paulson on 11 Sep 2020
Hi Dee,
Just wanted to let you know that I was having the exact same problem, though with a diary output file. I think my problem stems from the fact that I am developing on a Mac and then processing data on a Widnows 10 machine. Anyway, I tried a couple of things to get this working, and the fclose('all') suggestion didn't work for me.
I started off trying to rename a file in the MATLAB home directory before moving it to my output file folder, like...
diary filename
%...all the rest of my data processing code...
diary off
movefile filename newfilename.txt;
movefile('newfilename.txt', output_files_path);
I checked the file permissions for that folder too, and found that it had a modified read only attribute, i.e. the Read Only box for that folder's properties was colored in, as opposed to checked). Playing with the permsisions didn't seem to change anything, and it was actually set the same as other folders I know I can write to.
I also tried adding an 'f' to the end of the renaming movefile per the movefile documentation, as in:
movefile filename newfilename.txt;
That didn't change anything either. What worked was getting rid of the rename and just naming the file what I needed at the start, then moving the file, e.g.
diary newfilename.txt
%...all the rest of my data processing code...
diary off
movefile('newfilename.txt', output_files_path);
disp('All Finished!')
Seems like this might be a bug in movefile or they way it works with different operating systems isn't well undertood. Hope that helps!

Sign in to comment.

More Answers (0)

Categories

Find more on Programming 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!