move file to folder with the same name in matlab

I am new in matlab and I have some problem in moving file I have a list of files and I want to move them in folders with the same name%for example 1.txt ---> to folder 1 I have checked some codes but I have problems with the movefile command Thank you in advance

%load and save only txt files
%just the files in struct
ls_al = dir;
justfiles = ls_al(~[ls_al.isdir]); %only for files
%save only folders by removing . and ..
%folders in cell
d = dir(pwd);
isub = [d(:).isdir]; %# returns logical vector
nameFolds = {d(isub).name}';
nameFolds(ismember(nameFolds,{'.','..'})) = [];
% move file to folder
for i=1:numel(justfiles)
filename=fullfile(justfiles.name(i));
foldername=fullfile(nameFolds(i));
movefile(filename,foldername);
end

 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

??? Field reference for multiple structure elements that is followed by more reference blocks is an error.

Upvotes: 0

Views: 627

Answers (1)

matlabgui
matlabgui

Reputation: 5672

You have an error in your loop - you are indeding the justfiles variiable wrong:

filename=fullfile(justfiles.name(i));

this should be:

filename=justfiles(i).name;

Your use of fullfile here is incorrect as well - so I removed it. You are only passing in a single argument - see help fullfile for correct usage of providing folder(s) and filename.

Upvotes: 1

Related Questions