Open file in notepad, save file as *.txt, and close notepad

7 vues (au cours des 30 derniers jours)
Jesse Brown
Jesse Brown le 18 Juin 2020
Commenté : Walter Roberson le 18 Juin 2020
Hello,
I have an ASCII file format *.mca that is not recognized by Matlab. I know that I can open the files from the Matlab comand line with " !notepad Filename.mca". Is there a way to then save each file as a *.txt and close notepad. Ideally I would run a script with an input of some directery that has all of the *.mca files and then run a loop to save all the files as *.txt .
  4 commentaires
Stephen23
Stephen23 le 18 Juin 2020
This question seems to be based on the misunderstanding that changing the file extension will change MATLAB's ability to interpret some specific text file format. It won't: changing the file extension will not mean that MATLAB can suddenly interpret the file format.
Iny any case, MATLAB can open any text file using fopen, the extension does not have to be .txt. You can open .mca files just like any other text file:
fid = fopen('filename.mca','rt');
...
fclose(fid);
Walter Roberson
Walter Roberson le 18 Juin 2020
Opening in notepad and saving can have effects on newline line endings -- but MATLAB can handle both standards. Sometimes you might need to fopen() with 'rt' instead 'r' for some programs.
Opening in notepad and saving can have effects on character set encoding. R2020a onward can automatically handle UTF8 and UTF16. However, MATLAB will not necessarily automatically detect CP-1252 encoding. It is possible to get MATLAB to read CP-1252, and sometimes that is easy, and sometimes it takes a bit more work (depending how the file is being processed.)

Connectez-vous pour commenter.

Réponses (1)

Image Analyst
Image Analyst le 18 Juin 2020
If you want to do it manually, then do it manually in notepad. User manually changes text on a whim and manually saves it from the pulldown menu. No need for MATLAB at all.
If you want to have MATLAB alter the text, then there is no need to get notepad involved. Simply read in the file, make your alterations and write it back out again. These will be useful: fopen(), fgetl(), fprintf(), fclose().

Catégories

En savoir plus sur Workspace Variables and MAT Files dans Help Center et File Exchange

Produits


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by