<p>First, you could check if a variable with the given name is present. This could be done via <code>ncinfo</code>. I would consider it as good practice to check for the existence of a variable name in the first place instead of running into an error + catching it. If you want to go this way, the following answer might help you:</p>
<p><a href="https://de.mathworks.com/matlabcentral/answers/1989953-how-to-read-all-variables-in-netcdf-file#answer_1265028" rel="nofollow noreferrer">https://de.mathworks.com/matlabcentral/answers/1989953-how-to-read-all-variables-in-netcdf-file#answer_1265028</a></p>
<p>However, this does not answer your question.</p>
<p>MATLAB offers try-catch statements to deal with errors/exceptions. In your case, it could look like this:</p>
<pre><code>try
    data = ncread(&quot;example.nc&quot;,&quot;abc&quot;);
catch ME
    if (strcmp(ME.identifier, &quot;MATLAB:imagesci:netcdf:unknownLocation&quot;))
        disp(&quot;variable 'abc' does not exist&quot;)
    else
        rethrow(ME)
    end
end
</code></pre>
<p>What does this code do? If an error is thrown in the <code>try</code> section, we directly enter the <code>catch</code> section. <code>ME</code> is an instance of the error/exception thrown in the <code>try</code> section. Each error/exception has an <code>identifier</code> which is like the type/class of the error/exceptions (e.g.: file not found; permission denied; not a number; ...). If you want to find out which string you need to compare against <code>ME.identifier</code>, you can run code like this:</p>
<pre><code>try
    data = ncread(&quot;example.nc&quot;,&quot;abc&quot;);
catch ME
    disp(ME.identifier)
end
</code></pre>
<p>I am not 100% if I use the correct MATLAB wording because I did not use MATLAB in the past 13 years.</p>

CC BY-SA 4.0
2024-08-07T21:53:08.677Z
2024-08-07T21:53:08.677Z
daniel.heydebreck
<p>If an expected variable is not present in the .nc file, is there a way to catch the Matlab error without it crashing the script?</p>

<netcdf><matlab>
Catching a matlab ncread error?
Richard bouchard
# Catching a matlab ncread error?

(Asked by: username_0 on 2024-07-30)

If an expected variable is not present in the .nc file, is there a way to catch the Matlab error without it crashing the script?

**Tags:** netcdf, matlab


---

## 1 answers

### Answer by username_1 on 2024-08-07. Score: 1

First, you could check if a variable with the given name is present. This could be done via ncinfo. I would consider it as good practice to check for the existence of a variable name in the first place instead of running into an error + catching it. If you want to go this way, the following answer might help you:
https://de.mathworks.com/matlabcentral/answers/1989953-how-to-read-all-variables-in-netcdf-file#answer_1265028
However, this does not answer your question.
MATLAB offers try-catch statements to deal with errors/exceptions. In your case, it could look like this:
try
    data = ncread("example.nc","abc");
catch ME
    if (strcmp(ME.identifier, "MATLAB:imagesci:netcdf:unknownLocation"))
        disp("variable 'abc' does not exist")
    else
        rethrow(ME)
    end
end

What does this code do? If an error is thrown in the try section, we directly enter the catch section. ME is an instance of the error/exception thrown in the try section. Each error/exception has an identifier which is like the type/class of the error/exceptions (e.g.: file not found; permission denied; not a number; ...). If you want to find out which string you need to compare against ME.identifier, you can run code like this:
try
    data = ncread("example.nc","abc");
catch ME
    disp(ME.identifier)
end

I am not 100% if I use the correct MATLAB wording because I did not use MATLAB in the past 13 years.


---

