Write new file in different directory
8 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(1 个)
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 个评论
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 Center 和 File Exchange 中查找有关 File Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!