Save text file to a specific folder

46 ビュー (過去 30 日間)
Frederico
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');
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 件のコメント
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 Center および File ExchangeData Import and Export についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by