Copy a file *.m into newly made folder (folder5, folder6, folder....)

for n = 1:n
mkdir(['folder',sprintf('%d',n)])
end
path1= 'folder(num2str(n))'; %%???? Here I got error for the path name
dos(['copy *.m ', path1])

8 件のコメント

Rik
Rik 2021 年 4 月 23 日
But you are already using sprintf. What don't you understand? Did you do a basic Matlab tutorial?
Yogesh Bhattarai
Yogesh Bhattarai 2021 年 4 月 23 日
you didn't understand, i mean to copy *.m file to newly made folder. i want to specify correct code for path 1 (that is newly made folder from above sprintf) which is not right in above case giving error
Rik
Rik 2021 年 4 月 23 日
You show in your for loop that you know how to create a folder name. You use sprintf to create the folder name for mkdir. Why don't you do the exact same thing to create the path1 variable?
for n = 1:n
mkdir(sprintf('folder%d',n));
end
path1=sprintf('folder%d',n);
dos(['copy *.m ', path1])
Yogesh Bhattarai
Yogesh Bhattarai 2021 年 4 月 23 日
Thank you so much...
Yogesh Bhattarai
Yogesh Bhattarai 2021 年 4 月 23 日
and what should we change if we want to put two number such as folder50_25?
Stephen23
Stephen23 2021 年 4 月 23 日
編集済み: Stephen23 2021 年 4 月 23 日
Rather than very smelly copying of m-files into directories, most likely a much better approach is to use absolute/relative filenames when accessing data files.
Rik
Rik 2021 年 4 月 23 日
Have you read the documentation for sprintf? It should be easy for you to add a second number if you did.
You should also take the advice from Stephen to hart.
Yogesh Bhattarai
Yogesh Bhattarai 2021 年 4 月 24 日
THANKS for all your good sugestions

サインインしてコメントする。

 採用された回答

Image Analyst
Image Analyst 2021 年 4 月 23 日

0 投票

You should not do this:
for n = 1:n
It's very confusing (even though it works) because it appears that n is both the loop iterator and the ending iteration value and seems to take on two contradictory values. You should do this:
for k = 1 : n
To create a series of folders, do this:
n = 4; % Whatever...
pathList = cell(n, 1); % Preallocate a cell array to contain all the folder names.
for k = 1 : n
% Create the folder name as a character array.
folderName = fullfile(pwd, sprintf('Folder %2.2d', k));
if ~isfolder(folderName)
% Folder does not exist. Need to create it.
mkdir(folderName);
end
pathList{k} = folderName; % Save the folder name in a list.
end
path1= pathList{1}; % Or whatever you want...
path2= pathList{2}; % Or whatever you want...

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by