필터 지우기
필터 지우기

Error using exist "The optional second input to exist must be 'var', 'builtin','class', 'dir' or 'file' "

조회 수: 7 (최근 30일)
I don't understand why the following code doesn't work
for k = 1:4
% Create a text file name, and read the file.
textFileName = ['A' num2str(k) '.txt'];
if exist(textFileName, 'A')
fid = fopen(textFileName, 'rt');
mydata = fread(fid);
fclose(fid);
else
fprintf('File %s does not exist.\n', textFileName);
end
end
  댓글 수: 1
Steven Lord
Steven Lord 2019년 2월 19일
What do you think this does or want it to do?
if exist(textFileName, 'A')
You expect this if condition to be satisfied if ... ?
Once we know that we may be able to help you write the correct command.

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

채택된 답변

Adam Danz
Adam Danz 2019년 2월 19일
편집: Adam Danz 2019년 2월 19일
It looks like you're checking the existance of a certain file named something like "A1.txt" on the matlab path prior to reading that file. The exist() function has 1 mandatory input and 1 optional input. The optional input allows you to specify the type of result to search for. If you're searching for a file, one of those option for the 2nd input is 'file'.
The correct way to search for the existance of that file on the matlab path is:
if exist(textFileName, 'file')

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

태그

제품


릴리스

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by