Save text file to a specific folder

46 visualizzazioni (ultimi 30 giorni)
Frederico
Frederico il 7 Mag 2015
Modificato: Stephen23 il 14 Gen 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.

Risposte (1)

Abhiram Bhanuprakash
Abhiram Bhanuprakash il 7 Mag 2015
Modificato: Abhiram Bhanuprakash il 7 Mag 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 Commento
Elisa Michelini
Elisa Michelini il 13 Gen 2018
Modificato: Stephen23 il 14 Gen 2018
Abhiram Bhanuprakash I tried to use your code but unfortunately it did not work; I got the same error of Federico

Accedi per commentare.

Categorie

Scopri di più su Data Import and Export in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by