Borrar filtros
Borrar filtros

Save text file to a specific folder

20 visualizaciones (últimos 30 días)
Frederico
Frederico el 7 de Mayo de 2015
Editada: Stephen23 el 14 de En. de 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.

Respuestas (1)

Abhiram Bhanuprakash
Abhiram Bhanuprakash el 7 de Mayo de 2015
Editada: Abhiram Bhanuprakash el 7 de Mayo de 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 comentario
Elisa Michelini
Elisa Michelini el 13 de En. de 2018
Editada: Stephen23 el 14 de En. de 2018
Abhiram Bhanuprakash I tried to use your code but unfortunately it did not work; I got the same error of Federico

Iniciar sesión para comentar.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by