Save text file to a specific folder

조회 수: 24 (최근 30일)
Frederico . 2015년 5월 7일
편집: Stephen23 . 2018년 1월 14일
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.

답변 (1개)

Abhiram Bhanuprakash
Abhiram Bhanuprakash 2015년 5월 7일
편집: Abhiram Bhanuprakash 님. 2015년 5월 7일
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');
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,
  댓글 수: 1
Elisa Michelini
Elisa Michelini 2018년 1월 13일
편집: Stephen23 님. 2018년 1월 14일
Abhiram Bhanuprakash I tried to use your code but unfortunately it did not work; I got the same error of Federico

댓글을 달려면 로그인하십시오.


Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by