Problem getting error while trying to save .mat file in every folder

조회 수: 1 (최근 30일)
sam moor
sam moor 2017년 5월 22일
댓글: sam moor 2017년 5월 24일
I have 44 folders and I have a matlab script which runs for loop for every folder and save .mat file. But when I save .mat file inside the for loop, the matlab file doesn't save inside every folder but save in main folder. I want to save the mat file in every folder that I am running my script but it is not working. Is there any method to save .mat file in every folder of my for loop?

채택된 답변

Stephen23
Stephen23 2017년 5월 22일
편집: Stephen23 2017년 5월 22일
You need to tell MATLAB the absolute or relative path of the directory where you want to save those files, something like this (you can fill in the missing data yourself):
for k = 1:N
...
k_path = ...
k_name = ...
k_full = fullfile(k_path,k_name);
save(k_full,...)
end
In any case, how to process a sequence of files is covered in the documentation and this forum (and folders uses exactly the same methods):
etc etc
  댓글 수: 3
Stephen23
Stephen23 2017년 5월 22일
편집: Stephen23 2017년 5월 22일
You define your folder name. How you do this is up to you: you have not told us whether you have a list (e.g. a cell array) of folder names, or you want to generate them on-the-fly, or something else. If you do not tell us, then I have no idea how you define those names:
k_path = file path, e.g. from whatever cell array you defined
k_name = file name, e.g. from whatever cell array you defined
Perhaps you have something like this:
root_dir = ...
C = {...}; % directories
fnm = 'myfile.txt';
for k = 1:numel(C)
k_full = fullfile(root_dir,C{k},fnm);
...
end
But because you did not tell us anything about how your file and folder names are defined this is all just guessing.
In any case, the methods that I showed are what you need to use: once you have decided how your names are defined (e.g. generated using sprintf, or from dir, etc), then put those values into the code I showed you.
You will learn a lot more about MATLAB if you started experimenting and trying the code that I showed and linked to. If you expect complete working solutions for every simple task that you have then writing code is going to be very difficult.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by