Hi Chiara,
To convert a text file to a NetCDF file, you need to properly define the dimensions and variables in the NetCDF file. Based on your description, it seems like you have three columns: x, y coordinates, and data values, with 31332 rows.
Before you can write a variable to a netcdf file, you need to first set up the NetCDF file with the proper variable names, dimensions, etc. via "nccreate".
Here's how you can modify your code to create the NetCDF file and write the data:
% Create a NetCDF file
nccreate('myexample.nc', 'x', 'Dimensions', {'r', 31332});
nccreate('myexample.nc', 'y', 'Dimensions', {'r', 31332});
nccreate('myexample.nc', 'data', 'Dimensions', {'r', 31332});
% Write the data to the NetCDF file
x = % read x coordinates from your text file
y = % read y coordinates from your text file
data = % read data values from your text file
ncwrite('myexample.nc', 'x', x);
ncwrite('myexample.nc', 'y', y);
ncwrite('myexample.nc', 'data', data);
% Display the NetCDF file structure
ncdisp('myexample.nc');
By following this approach, you should be able to create a NetCDF file with the desired dimensions and variables and write the data into it.
Hope this helps!