Error using fprintf, Invalid file identifier

2 ビュー (過去 30 日間)
vitor gabriel
vitor gabriel 2017 年 11 月 27 日
コメント済み: vitor gabriel 2017 年 11 月 28 日
Hello, I'm trying this code:
% log file:
if(write)
logid = fopen(logfilename, 'w');
%fprintf(logid, 'Induction times for constant-volume explosion\n');
fprintf(logid, 'burned gas composition\n');
fprintf(logid, 'fH, p, Tu');
for(i=1:length(ynames))
fprintf(logid,', ');
fprintf(logid, ynames{i});
end
fprintf(logid,'\n');
end
But I'm having this error:
Error using fprintf
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in lookupY_05 (line 48)
fprintf(logid, 'burned gas composition\n');
I tryied to follow some tips from similar questions but it's not working. Thanks a lot for any help.
Bye
  2 件のコメント
Chafik Zerrouki
Chafik Zerrouki 2017 年 11 月 27 日
It's very difficult to help you with your code writed like this. Be sure to be lisible when you ask a question to be lisible.
Perhaps, the problem is it with 'logfilename'. Did you write it like that ?
logfilename = 'myText.txt';
vitor gabriel
vitor gabriel 2017 年 11 月 27 日
Hello, I wrote as logfilename = [dir writetofilename '.log']; % full logfile name

サインインしてコメントする。

採用された回答

Guillaume
Guillaume 2017 年 11 月 27 日
Your code failed to open the file for writing. There can be many reasons for that which we can only take a guess at. Maybe the file doesn't exist, maybe you don't have permission to write it, maybe something else.
To know for sure:
[logid, errmsg] = fopen(logfilename, 'w');
assert(logid > 0, 'failed to open %s for writing because %s, logfilename, errmsg)
  3 件のコメント
Guillaume
Guillaume 2017 年 11 月 27 日
There is no point showing us more code. As I said, the file cannot be created for some reason. This is specific to your machine. Perhaps the path you're using does not exist. Perhaps you don't have write access to that path, perhaps something else.
The code I've given you will tell you exactly what the problem is.
vitor gabriel
vitor gabriel 2017 年 11 月 28 日
Thank you, I solved. It was a very stupid mistake. Have a good day

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeAxis Labels についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by