Error: Undefined function or variable 'fgetl' when fgetl() is placed inside an else statement

6 ビュー (過去 30 日間)
Hi, I'm writing a simple script to read a .csv file. I get the error:
Undefined function or variable 'fgetl'.
Error in (filename) (line 36)
line = fgetl(fid);"
It's strange because the call to fgetl() outside the if statement works fine!
if(~exist(file_in))
FInputErrMsg = 'not located in working directory.';
Suggestion = ['Change script parameter or move the file into script'...
' working directory.'];
error('Expected input file: "%s" %s\n%s',file_in,FInputErrMsg,Suggestion);
elseif(exist(file_out))
% TODO#1
end
% Open file_in1 to count the number of sites N
fid = fopen(file_in,'r');
line = fgetl(fid);
if(fid == -1)
error('Error opening %s. Exiting.', file_in);
else
line = fget1(fid); %<---------------------- Offending Line
end
fclose(fid);

採用された回答

Steven Lord
Steven Lord 2017 年 7 月 16 日
Check in your original code whether you're calling fgetl with a lower-case L or calling fget1 with the number one. The code segment you posted implies the latter, the error message you posted implies the former.
  2 件のコメント
Minh Tran
Minh Tran 2017 年 7 月 16 日
編集済み: Minh Tran 2017 年 7 月 16 日
*Tear out hair* I apologize. In my haste, I must have tried both lowercase L and the number one but copied the version with the number one onto my clipboard.
Looking at the editor, the lowercase L and the number 1 looks AWFULLY similar.
dpb
dpb 2017 年 7 月 16 日
OK, but you've still got the error test for fopen out of order as written above...

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

その他の回答 (1 件)

dpb
dpb 2017 年 7 月 16 日
fid = fopen(file_in,'r');
line = fgetl(fid);
if(fid == -1)
error('Error opening %s. Exiting.', file_in);
...
You've got the test for successful fopen out of order; that should be before you try to use it; then the fgetl call doesn't need to be inside the if construct at all. Then, it appears your fopen did fail so you called fgetl(-1) which then looks like an array reference instead of a function call since -1 isn't a valid file handle.
fid = fopen(file_in,'r');
if(fid<0), error('Error opening %s. Exiting.', file_in); end
line = fget1(fid);
fclose(fid);
...

カテゴリ

Help Center および File ExchangeWorkspace Variables and MAT Files についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by