Renaming files using MATLAB
61 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
MathWorks Support Team
el 28 de Jul. de 2020
Editada: MathWorks Support Team
el 3 de Feb. de 2025
How can I rename files on my computer using MATLAB?
I have many files on my computer and want to use MATLAB to rename them all at once.
Respuesta aceptada
MathWorks Support Team
el 17 de En. de 2025
Editada: MathWorks Support Team
el 3 de Feb. de 2025
From MATLAB, you can use the "movefile" function to rename files on your machine. To access the specific documentation for this function in MATLAB R2020a, please run the following command in the command window:
>> web(fullfile(docroot, 'matlab/ref/movefile.html'))
For more information on managing files and folders in MATLAB, refer to the documentation by executing the following command in MATLAB R2020a:
>> web(fullfile(docroot, 'matlab/matlab_env/manage-files-and-folders.html'))
Alternatively, you can use the "system" command which executes on the local operating system.
On Windows using CMD, the "rename" command works like this:
C:> rename file_path new_name
C:> rename C:\Temp\file1.txt file2.txt
So in MATLAB, from the directory with the file you could execute:
>> system("rename " + "old_name.txt" + " " + "new_name.txt")
If the file names contain spaces, this can confuse the operating system, so a more robust approach is to wrap each file name in double quotes like this:
>> system("rename " + '"' + "old name.txt" + '" "' + "new name.txt" + '"')
This produces a system command like this:
C:> rename "old name.txt" "new name.txt"
To use MATLAB variables for the old and new file names you can use these MATLAB commands:
>> old_path_to_file = "C:\Temp\test a.txt";
>> new_filename = "test b.txt";
>> cmd = "rename " + '"' + old_path_to_file + '" "' + new_filename + '"';
>> system(cmd);
On a Linux or macOS (Unix) system, this would be:
>> old_path_to_file = "/tmp/test a.txt";
>> new_filename = "test b.txt";
>> cmd = "mv " + '"' + old_path_to_file + '" "' + new_filename + '"';
>> system(cmd);
Please follow the below link to search for the required information regarding the current release:
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Startup and Shutdown en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!