2
$\begingroup$

I've already read a Q&A stating that because NetCDF does not support primitive string type variables, that it is impossible to export a string to a NetCDF file.

For example:

testvalsnodvar = {"VOL_FRACTION01"}; Export["W:\\LPF_extractor\\test.nc", {"vals_nod_var" ->testvalsnodvar}, "Datasets"]

gives a Java error stating that this is an incorrect format for Datasets. Is there a way around this or am I stuck?

$\endgroup$
3
$\begingroup$

Convert the string to a list of integers using ToCharacterCode, and then convert back using FromCharacterCode.

testvalsnodvar = {"VOL_FRACTION01"};
Export["test.nc", {"vals_nod_var" -> 
   ToCharacterCode@testvalsnodvar}, "NetCDF"]
(* "test.nc" *)

Import[
  "test.nc", {"Datasets", "vals_nod_var"}] // FromCharacterCode
(* {"VOL_FRACTION01"} *)
$\endgroup$
  • $\begingroup$ That works great for working strictly in Mathematica, however, reading this into another program does not work so well. I suppose there is no way around it if my only option is to convert strings into ASCII code. $\endgroup$ – plasmacaster Apr 30 '16 at 0:33
  • $\begingroup$ From what this page says strings are not a natively supported data type. $\endgroup$ – Jason B. Apr 30 '16 at 6:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.