필터 지우기
필터 지우기

Why do I get an error when I try to write data into file?

조회 수: 1 (최근 30일)
Hannah
Hannah 2021년 8월 12일
댓글: Hannah 2021년 8월 12일
I want to write "Formattyp = 1" into my file Batch.csv.
This is the section of my code:
myBatchName = '\\\\data-be\\data-ti-2019\\eit\\50_Labore\\T016-Photovoltaik_1\\06_Projekte\\02_Aktiv\\2019_Schenker_Storen\\DOCS_Hannah\\Experiment2\\02_Generate_Batch_File\\02-EXP2_Batch_Files\\Batch.csv';
fileID = fopen(myBatchName,'w'); %open file for writing; discard existing contents
fprintf(fileID,'Formattyp = 1\n');
But I keep getting this error:
" Error using fprintf
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in EXP2_Ver1_Generate_Batch_File (line 111)
fprintf(fileID,'Formattyp = 1\n'); "
Which I do not understand because I have specified my fileID, so it should know where to write the data into. What am I doing wong? Thanks.
  댓글 수: 2
Stephen23
Stephen23 2021년 8월 12일
편집: Stephen23 2021년 8월 12일
"...so it should know where to write the data into."
And what should MATLAB do if that path or file does not exist? Specifying the file ID does not tell us anything about if the path exists. Rather than just guessing what the problem is, it is much simpler to get FOPEN to tell you why it could not open that file:
[fileID,msg] = fopen(myBatchName,'w');
assert(fid>=3,'%s',msg)
Hint: get rid of those duplicated backslashes, and don't forget FCLOSE at the end.
Hannah
Hannah 2021년 8월 12일
Thanks, yeh it works when I remove the duplicated backslashes.
Now i'm wondering why sometimes duplicated backslashes are needed when specifying a path e.g. when 'sprintf' is used. How can I know when to use duplicated backslashes?

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

채택된 답변

KSSV
KSSV 2021년 8월 12일
Check the path of the file....check the value of fileID. If the file is not open properly, the value will be negative. There is probem with your filename/ path.

추가 답변 (0개)

카테고리

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