Returning an error with my "if" statement.
이전 댓글 표시
I am trying to run this part of my code and it is returning an error saying that the specified file does not exist.
fid = netcdf.open(fname, 'NC_NOWRITE');
if fid == -1;
['No Interpolated Winds Present']
exit
end
This is the error:
% Get the full path name.
fid = fopen(filename, 'r');
if fid == -1;
error ('MATLAB:netcdf:open:noSuchFile', ...
'The specified file does not exist.');
end
The command window says: ??? Error using ==> open at 44. The specified file does not exist.
What is frustrating is that this worked for me yesterday. Anyway, is there another way to tell MATLAB to not use the value if it is equal to -1. Any help would be appreciated.
Thanks, Emily
답변 (2개)
Honglei Chen
2013년 5월 29일
1 개 추천
I may be wrong but this looks to me has nothing to do with MATLAB. It errors out because the file was not found. Did you move your file around?
Wayne King
2013년 5월 29일
Honglei is absolutely correct. Perhaps you have not added the folder where the file exists to the MATLAB path? If that is the case, use addpath() or pathtool to add the folder.
If your question is more general about how to keep executing code when there is an error, then you can use a try-catch
try
ncid = netcdf.open('test.nc','NOWRITE');
catch ME
disp('This still executes even though there is an error');
end
The error is captured in ME, but the code between the catch and the end statement executes (as long as there are no errors in that block).
However, in this use case I'm not sure how much use that will be since the error is that you are not able to open your file. Presumably you'll need that for any subsequent processing.
댓글 수: 3
Emily
2013년 5월 29일
Wayne King
2013년 5월 29일
편집: Wayne King
2013년 5월 29일
If you enter
>>which fname
what do you get back? Does it say that fname is a variable?
Then if you query the actual string it represents, what do you get?
Emily
2013년 5월 29일
There was a problem with the variable fname, but when I did what you suggested, it does say that fname is a variable and it represents a specified file. I ended up just using the actual file, instead of fname, to evaluate the section and it worked. I had done that before posting this question and it was still showing error, which was what prompted me to ask the question. But in any case the problem seems to be rectified. I greatly appreciate your time and help on this. Thank you!
카테고리
도움말 센터 및 File Exchange에서 Data Import and Export에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!