필터 지우기
필터 지우기

Write new file in different directory

조회 수: 7 (최근 30일)
Nidhi SRIVASTAVA
Nidhi SRIVASTAVA 2017년 7월 22일
댓글: Walter Roberson 2017년 7월 22일
I want to create a new file in a directory other than the current opened directory.
My code:
f='somefilename'
fid = fopen(f,datadirectory,'w');
where datadirectory is the directory where I want to write my file. But it throws an error.
Can somebody please help? Thank you.

답변 (1개)

Walter Roberson
Walter Roberson 2017년 7월 22일
filename = fullfile(datadirectory, f);
fid = fopen(filename, 'w');
Note: you mention "text file" in the tags. If you are using MS Windows then it would be common to use 'wt' instead of 'w' when created text files. Using 'wt' tells it to use Carriage Return / Linefeed pairs for the line, which is needed if you want compatibility with older programs such as Notepad
  댓글 수: 2
Nidhi SRIVASTAVA
Nidhi SRIVASTAVA 2017년 7월 22일
Hi. Thanks for the answer but it still shows the same error. I want to save the file in the different directory. What I am guessing is matlab gives an error because it can't read the file in the working directory. Error: Invalid file identifier. Use fopen to generate a valid file identifier.
Walter Roberson
Walter Roberson 2017년 7월 22일
You should use
filename = fullfile(datadirectory, f);
[fid, msg] = fopen(filename, 'w');
if fid < 0
error('Failed to open file "%s" because: "%s"', filename, msg);
end

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

카테고리

Help CenterFile Exchange에서 Low-Level File I/O에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by