Try this -
folderName = 'all';
mkdir(strcat(folderName,filesep,'temp'));
N = 10; %%// Number of files needed
for k = 1:N
temp = ['x', num2str(k), '.txt'];
pathname1 = strcat(pwd,filesep,folderName,filesep,'temp',filesep,temp);%%// creates the temp directory in the working directory.
fid1 = fopen(pathname1, 'w');
fprintf(fid1, '%s\n',M{:});
fclose(fid1);
end
Thus, the files x1.txt
, x2.txt
, x3.txt
and so on would be created inside the directory temp
that is inside a directory all
and all
is inside the working directory.
If you would like to create this directory all
elsewhere, mention the full path for folderName
at the start and edit the pathname1
creation, like this -
pathname1 = strcat(folderName,filesep,'temp',filesep,temp);