Save text file to a specific folder

46 vues (au cours des 30 derniers jours)
Frederico
Frederico le 7 Mai 2015
Modifié(e) : Stephen23 le 14 Jan 2018
Hi. I am trying to save a text (.txt) file into a path different to Matlab's. I am manipulating a text file Matlab where I am using the following code:
file = importdata('originalname.txt')
% do all my manipulation using file {x,x} = ' new phrase '
fid = fopen('newname.txt', 'wt'); fprintf(fid, '%s\n', file{:}); fclose(fid);
My question is that I want to save the newname.txt to C:\something\somethingelse , instead of the usual Matlab path folder. I have tried fid = fopen('C:\something\somethingelse\newname.txt', 'wt') , but I get:
Error using fprintf. Invalid file identifier. Use fopen to generate a valid file identifier.
Anyone mind helping me out with this? Thanks in advance.

Réponses (1)

Abhiram Bhanuprakash
Abhiram Bhanuprakash le 7 Mai 2015
Modifié(e) : Abhiram Bhanuprakash le 7 Mai 2015
Hi Frederico,
In the documentation for fopen here , you can see that fopen creates a new file if the file does not exist.
I tried doing the way you did. That is:
fid = fopen('C:\abcd.txt','w+');
fprintf(fid, '%s\n', 'SampleText');
fclose(fid)
But I got the expected result without any errors.
Probably you can check if there is any error message with this syntax of fopen:
[fileID,errmsg] = fopen(___)
As mentioned in the doc, the above returns a system-dependent error message if fopen fails to open the file.
Also you can try observing the fid value which is returned,
fid = -1 means it cannot open file
fid = 1 means standard output
fid = 2 means standard error
as per this page
You can comment on this if you would like to share something more about your workflow.
Hope this helps,
Abhiram.
  1 commentaire
Elisa Michelini
Elisa Michelini le 13 Jan 2018
Modifié(e) : Stephen23 le 14 Jan 2018
Abhiram Bhanuprakash I tried to use your code but unfortunately it did not work; I got the same error of Federico

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Import and Export dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by