%% Create New NetCDF File From Existing File or Template
% This example shows how to create a new NetCDF file that contains the variable, dimension, and group definitions of an existing file, but uses a different format.
% Create a file containing one variable, using the nccreate function.

nccreate('myfile.nc','myvar')

%
% Write data to the file.

A = 99;
ncwrite('myfile.nc','myvar',A)

%
% Read the variable, dimension, and group definitions from the file using ncinfo. This information defines the file's schema.

S = ncinfo('myfile.nc');

%
% Get the format of the file.

file_fmt = S.Format

%
% Change the value of the Format field in the structure, S, to another supported NetCDF format.

S.Format = 'netcdf4';

%
% Create a new version of the file that uses the new format, using the ncwriteschema function. A schema defines the structure of the file but does not contain any of the data that was in the original file.

ncwriteschema('newfile.nc',S)
S = ncinfo('newfile.nc');

% Note: When you convert a file's format using ncwriteschema, you might get a warning message if the original file format includes fields that are not supported by the new format. For example, the netcdf4 format supports fill values but the NetCDF classic format does not. In these cases, ncwriteschema still creates the file, but omits the field that is undefined in the new format.
%
% View the format of the new file.

new_fmt = S.Format

% The new file, newfile.nc, contains the variable and dimension definitions of myfile.nc, but does not contain the data.
%
% Write data to the new file.

ncwrite('newfile.nc','myvar',A)

% Copyright 2015 The MathWorks, Inc.