An error using fprintf

1 回表示 (過去 30 日間)
EK
EK 2020 年 3 月 2 日
編集済み: Guillaume 2020 年 3 月 2 日
Hello,
I am currently getting an error while trying to save. This is what I currently have
function saveline(filename,line,mode)
fid = fopen(filename,mode);
fprintf(fid,line);
fclose(fid);
end
Then I get
Error using fprintf
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in saveline (line 11)
fprintf(fid,line);
Any suggestions?

回答 (1 件)

Guillaume
Guillaume 2020 年 3 月 2 日
編集済み: Guillaume 2020 年 3 月 2 日
Any suggestions?
Yes, for some reason, fopen failed to open the file. There can be many reasons, the most likely being that the file doesn't exist but other reasons are possible (permission problem, etc.)
You never check that the fopen succeeds which is never a good idea.
[fid, errmsg] = fopen(filename, mode);
if fid < 0
error('Failed to open "%s", the error message was: %s');
end
%...
would at least tell you what the problem is.

カテゴリ

Help Center および File ExchangeLow-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