% % This is the name of the data file we will read. FILE_NAME='simple_xy.nc'; % % We are reading 2D data, a 6 x 12 grid. NX=6; NY=12; % % Open the file. NC_NOWRITE tells netCDF we want read-only access % to the file. [ncid, status] = mexnc ( 'OPEN', FILE_NAME, nc_nowrite_mode ); if status, error(mexnc('STRERROR',status)), end % % Get the varid of the data variable, based on its name. [varid, status] = mexnc ( 'INQ_VARID', ncid, 'data' ); if status, error(mexnc('STRERROR',status)), end % % Read the data. [data_in, status] = mexnc( 'GET_VAR_INT', ncid, varid ); if status, error(mexnc('STRERROR',status)), end % % Remember to transpose! data_in = data_in'; % % Check the data. See simple_xy_wr.m for reference. data_out = [0:NX*NY-1]; data_out = reshape(data_out,NY,NX); data_out = data_out'; data_out = int32(data_out); if any(data_in-data_out) error ( 'Data read in by GET_VAR_INT did not match' ); end % % Close the file, freeing all resources. status = mexnc ( 'CLOSE', ncid ); if status, error(mexnc('STRERROR',status)), end fprintf(1, '*** SUCCESS reading example file %s with vanilla mexnc!\n', FILE_NAME); % % BUT WAIT!!! % The above process is even easier if you have SNCTOOLS installed. data_in = nc_varget ( FILE_NAME, 'data' ); % % Just remember that SNCTOOLS will assume some defaults, such as % data reads being either double precision or char. if any(data_in-double(data_out)) error ( 'Data read in by nc_varget did not match' ); end fprintf(1, '*** SUCCESS reading example file %s with SNCTOOLS!\n', FILE_NAME);