Returning an error with my "if" statement.
Show older comments
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
Answers (2)
Honglei Chen
on 29 May 2013
1 vote
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
on 29 May 2013
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 Comments
Emily
on 29 May 2013
Wayne King
on 29 May 2013
Edited: Wayne King
on 29 May 2013
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
on 29 May 2013
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!
Categories
Find more on Data Import and Export in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!