How to copy and rename a .mat file in many different folders

9 views (last 30 days)
I have a lot of Folders within one folder. Each of these different folders have a specific name. In each folder is a .mat file with the same name. I would like to write a function to copy this .mat file into it's own folder. And then rename each of the .mat files to match the name of its folder. This way I know what it came from and also easily tell them apart for easy re-usage in other scripts.
Main_folder/folder1/original.mat would become original.mat and folder1.mat. In folder2 there is also a file named original.mat that needs to be copied and the copy to be renamed folder2.mat. For folder3, etc. the same thing off course.
I'm starting from scratch because movefile didn't do the trick (yet).
My Matlab version is R2014b for Mac.
Many thanks! Stefan

Answers (1)

Riya
Riya on 1 Feb 2024
Hi
You can create a MATLAB function to automate the process of copying and renaming the `.mat` files in each subfolder within your main folder. Here's a step-by-step guide on how to do this:
1. Navigate to the main folder containing all the subfolders.
2. List all the subfolders within the main folder.
3. Loop through each subfolder, perform the following actions:
- Copy the `original.mat` file to the same folder.
- Rename the copied file to match the folder's name.
Below is a MATLAB function that does exactly that:
function copy_and_rename_mat_files(main_folder)
% Check if the main folder exists
if ~isfolder(main_folder)
error('The specified main folder does not exist.');
end
% Get a list of all subfolders within the main folder
subfolders = dir(main_folder);
subfolders = subfolders([subfolders.isdir]); % Keep only directories
subfolders = subfolders(~ismember({subfolders.name}, {'.', '..'})); % Remove '.' and '..' directories
% Loop through each subfolder
for k = 1:length(subfolders)
subfolder_name = subfolders(k).name;
subfolder_path = fullfile(main_folder, subfolder_name);
% Define the source and destination file paths
original_file = fullfile(subfolder_path, 'original.mat');
new_file = fullfile(subfolder_path, [subfolder_name '.mat']);
% Check if the original.mat file exists in the subfolder
if isfile(original_file)
% Copy and rename the file
copyfile(original_file, new_file);
fprintf('Copied and renamed %s to %s\n', original_file, new_file);
else
warning('File original.mat not found in %s. Skipping folder.', subfolder_path);
end
end
end
To use this function, simply call it with the path to your main folder as the argument:
copy_and_rename_mat_files('/path/to/Main_folder');
Make sure to replace `/path/to/Main_folder` with the actual path to your main folder.
This function will iterate through each subfolder in `Main_folder`, copy the `original.mat` file if it exists, and rename the copy to match the folder's name. If `original.mat` does not exist in a subfolder, it will print a warning message and continue with the next subfolder.

Categories

Find more on Entering Commands in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!