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

2 views (last 30 days)
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?

Accepted Answer

Stephen23
Stephen23 on 22 May 2017
Edited: Stephen23 on 22 May 2017
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 Comments
Stephen23
Stephen23 on 22 May 2017
Edited: Stephen23 on 22 May 2017
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Search Path in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!