MATLAB: How to copy a folder into another folder

copyfolders copyfile

In my current folder I have created two empty folders, one is "Folder1" and the other is "Folder2" and I want to copy one of these folders to another one by writing code in a matlab script. I used both of these commands:
copyfile('Folder1','Folder2')
copyfile('Folder1','C:\Users\st1azamiki\Desktop\new\Folder2')
But these commands do not copy the Folder1 inside the Folder2. Do not produce an error also. What am I doing wrong? How can I copy a Folder from a particular path to another particular path?

Best Answer

copyfile('Folder1', 'Folder2') copies the contents of Folder1 into Folder2:
cd(tempdir);
mkdir('Folder1')
mkdir('Folder2')
fid = fopen('Folder1\test.txt', 'w');
fclose(fid);
copyfile('Folder1', 'Folder2')
dir('Folder2')
Now Folder2 contains a copy of test.txt. But you want Folder2 to contain an instance of Folder1. Then do this explicitly:
copyfile('Folder1', 'Folder2\Folder1')
Now the contents of the (empty or non-empty) Folder1 is copied to Folder2\Folder1.
I used relative path names here for compactness. The problem of your command was not the absolute path, but the missing of the name of the destination folder. In productive code a GUI or timer callback can change the current folder, therefore I use absolute path names in every case:
Folder1 = fullfile(tempdir, 'Folder1');
Folder1 = fullfile(tempdir, 'Folder2');
copyfile(Folder1, fullfile(Folder2, 'Folder1'));