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

Réponses (2)

Honglei Chen
Honglei Chen le 29 Mai 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
Wayne King le 29 Mai 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 commentaires

Emily
Emily le 29 Mai 2013
Thank you both (Honglei and Wayne) for your prompt answers. I am able to use the netcdf file in a previous set of code (the file is in my directory), so I don't think that is the problem. It is saying that the error is occurring at the statement 'No Interpolated Winds Present,' so does that mean it thinks this is a file I'm trying to open? Thanks again for your help.
Wayne King
Wayne King le 29 Mai 2013
Modifié(e) : Wayne King le 29 Mai 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
Emily le 29 Mai 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!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Import and Export dans Centre d'aide et File Exchange

Question posée :

le 29 Mai 2013

Community Treasure Hunt

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

Start Hunting!

Translated by