How to change .txt format automatically into .m format

Dear all:
I have a lot of .txt files which I want to change them automatically into .m format files, in order for some later processing in Matlab.
Any guide will be very appreciated.

1 comentario

You can do this by sending the appropriate command to the operating system. In Windows:
!ren *.txt *.m
will rename all *.txt files in your working directory to *.m

Iniciar sesión para comentar.

Respuestas (3)

Sibghat
Sibghat el 28 de Feb. de 2024
Movida: Steven Lord el 28 de Feb. de 2024
try the following...
% Define the directory containing the .txt files
directory = 'path/to/your/files/';
% List all .txt files in the directory
txtFiles = dir(fullfile(directory, '*.txt'));
% Loop through each .txt file
for i = 1:numel(txtFiles)
% Get the file name and extension
[~, filename, ext] = fileparts(txtFiles(i).name);
% Generate the new file name with .m extension
newFilename = [filename '.m'];
% Rename the file
movefile(fullfile(directory, txtFiles(i).name), fullfile(directory, newFilename));
% Display confirmation message
disp(['Converted ' txtFiles(i).name ' to ' newFilename]);
end
It will search the folder for text files and will change the format to M-file...
Bhupesh Soni
Bhupesh Soni el 19 de Abr. de 2020
!ren *.txt *.m

6 comentarios

we have to what with this?
if you enter this in matlab's command line then the text files in your currect directory will convert to .m files
Sergio
Sergio el 28 de Feb. de 2024
Editada: Sergio el 28 de Feb. de 2024
in MATLAB 2023b:
!ren *.txt *.m
/bin/bash: line 1: ren: command not found
Alexander
Alexander el 28 de Feb. de 2024
!ren is a windows command. If you use another operating system use the appropriate command, e.g. move.
The MacOS and Linux equivalent
!mv *.txt *.m
would try to expand all (existing) files ending in .txt and all files ending in .m, as-if they had been typed into the command -- for example as-if it had been entered
!mv file1.txt file2.txt file3.txt rename_my_files.m rename_helper.m
the last of the .m files would then be interpreted as the target file to move everything else to... so first file1.txt would be moved to rename_helper.m then file2.txt would be moved to rename_helper.m and so on, until finally rename_my_files.m would be renamed to rename_helper.m -- leaving you with just rename_helper.m with content the old rename_my_files.m
Unfortunately, the unix way of doing this rename is notably more messy.
Alexander
Alexander el 28 de Feb. de 2024
Thank you for this explanation. To be honest, the time I used unix is more than 30 years ago. I only remembered that "ren" is not a c-shell command. Hopefully is annoyed by my comment.

Iniciar sesión para comentar.

I think the easiest way is to use the "movefile" command with a for loop
for i=1:N_files % number of files
movefile(['file_number' str2num(i) '.txt'], ['file_number' str2num(i) '.m']) ; %This will convert them directly from .txt to .m and it can also be applied to other extensions
end

Categorías

Más información sobre File Operations en Centro de ayuda y File Exchange.

Preguntada:

el 12 de En. de 2017

Respondida:

el 8 de Ag. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by