MATLAB: Error using movefile: Arguments must contain a character vector.

MATLABmovefile

I have a whole bunch of images that I need to sort and save based on their study, name and date, which is there in the name of the images itself, eg "ABC_20180712_121343_L_001.png", where the 2nd portion is the date, 3rd being the time stamp, 4th being the criteria of the study and 5th is the image number. So, I create a new folder reading the name, criteria and date, eg "L_20180712_121343", but time only in cases where there are two images of the same date. After that I try to move the image to that folder, but matlab shows the error "Error using movefile: Arguments must contain a character vector." Here is part of whole code:
for k=3:length(dir_L)
name_img = dir_L(k,1).name; %read the names
name_1 = strsplit(name_img,{'_','.'});
name = name_1{1};
date = name_1{2};
time = name_1{3};
study = name_1{4};
if k==3
newfolder=strcat(study,'_',date); %naming the new folder
mkdir(newfolder); %creating the folder
end
img = imread(name_img);
movefile (img, newfolder); %transfering the images to the folder
end
I don't know what am I doing wrong.

Best Answer

Read the movefile help: does it say that its first input should be some image data? (hint: no). Does it say that its first input should be a filename or foldername? (hint: yes). That error message tells you what is wrong (which is why you should pay attention to what error messages actually say).
Get rid of the imread, and then use movefile according to its documentation:
movefile(name_img, newfolder)