How to save a column of strings into a NetCDF file?

8 views (last 30 days)
If I have a one-column data with a dimension of 200x1 (e.g., Longitude as in the below example), I know how to store them into a NetCDF file by following the below script:
%% define the dimensions:
dim = netcdf.defDim(ncid, 'data_grid', 200);
%% define the variable and variable attributes:
Var_Longitude = netcdf.defVar(ncid, 'LONGITUDE', 'double', dim);
netcdf.putAtt(ncid, Var_Longitude, 'long_name', 'Longitude in decimal degrees');
netcdf.putVar(ncid, Var_Longitude, Longitude);
What if I have a one-column text string with a dimension of 200x1? For example, the string matrix could be like this:
"test1"
"test2"
"test56"
...
Just change the 'double' to 'string"? Anything else I need to change?
  1 Comment
Leon
Leon on 10 Mar 2020
Many thanks for the detailed explanation!
For folks like myself who are not familiar with the differences of string vs char, this is super helpful. I appreciate the tricks to handle trailing spaces.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 10 Mar 2020
Edited: Walter Roberson on 10 Mar 2020
You need to char() the string matrix into a char array of constant width, and then you write the 2D char array.
using char() on a string array will blank pad the ends of shorter lines. If some of your strings end in whitespace that are important to preserve, then the opposite operation, string(cellstr(TheCharacterArray)) will trim those blanks out.
If that is not acceptable, you would need to choose a terminator character that you are certain will not occur otherwise, and put it on the end of shorter lines. Something like
S=["test1";"test2";"test567"];
Sc = char(S+'~');
Sc(:,end) = []; %no need for terminator on longest lines
and reverse would be
S_rebuilt = string(regexprep(cellstr(Sc), '~.*$', ''));
This is only needed if you have trailing whitespace you need preserved. Otherwise:
Sc = char(S);
S_rebuilt = string(cellstr(Sc));
Be careful, by the way, as to which dimension gets written first when you write a 2D array of char. You might need to write Sc.' (transpose) to match the standard use inside netcdf, and you might need to transpose when you read it in again,
S_rebuilt = string(cellstr(Sc.'));

More Answers (0)

Tags

Products

Community Treasure Hunt

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

Start Hunting!