3
$\begingroup$

The following Matlab code create_cdf.m:

A = reshape(1:32, 8,4);
cdfwrite('A.cdf',{'A', A});
A

creates A matrix of 8 rows and 4 cols, writes it into CDF file and prints:

>> create_cdf

A =

     1     9    17    25
     2    10    18    26
     3    11    19    27
     4    12    20    28
     5    13    21    29
     6    14    22    30
     7    15    23    31
     8    16    24    32

And the following Mathematica notebook read_cdf.nb:

SetDirectory[NotebookDirectory[]];

data = Import["A.cdf", "Elements"]
(* {"Annotations", "Data", "DataEncoding", "DataFormat", Datasets", "Metadata"} *)

data = Import["A.cdf", "Data"]
(* {{{{1., 2., 3., 4.}, {5., 6., 7., 8.}, {9., 10., 11., 
    12.}, {13., 14., 15., 16.}, {17., 18., 19., 20.}, {21., 22., 23., 
    24.}, {25., 26., 27., 28.}, {29., 30., 31., 32.}}}} *)

Dimensions[data]
(* {1, 1, 8, 4} *)

data = data[[1, 1]]
(* {{1., 2., 3., 4.}, {5., 6., 7., 8.}, {9., 10., 11., 
  12.}, {13., 14., 15., 16.}, {17., 18., 19., 20.}, {21., 22., 23., 
  24.}, {25., 26., 27., 28.}, {29., 30., 31., 32.}} *)

Dimensions[data]
(* {8, 4} *)

data // TableForm

enter image description here

I.e. returned data has the same shape, but incorrectly filled.

UPDATE

1) If Mathematica would interpret that CDF as Computable Document Format, it would return Notebook. Since it returns something different, it is correctly recognizes Common Data Format.

2) Matlab was used just as an example. I can't check Mathematica with itself, usage of other piece of software is unavoidable. I think the same results as Matlab is given by Python + CDF C. May be need to check additionally.

$\endgroup$
11
  • $\begingroup$ Do not use the tag bugs. It is reserved for community use. It will be applied to your question when and only when it has been verified that the issue you raise is a Mathematica bug and not your bug. $\endgroup$
    – m_goldberg
    Aug 18, 2017 at 17:08
  • $\begingroup$ I don't have Matlab, so I have no idea what the file "A.cdf" looks like. Perhaps you could include a link to the file, or the output of Import["A.cdf", String] //Compress so that we can inspect the contents of the file. $\endgroup$
    – Carl Woll
    Aug 18, 2017 at 17:17
  • $\begingroup$ You can put the textual content of a sample CDF file in Pastebin for easier sharing. $\endgroup$ Aug 18, 2017 at 17:24
  • 1
    $\begingroup$ In Mathematica, the CDF file extension mean "Computable Document Format", not "Common Data Format". This suggests you are confusing two different files format as the having the same format. $\endgroup$
    – m_goldberg
    Aug 18, 2017 at 17:52
  • 2
    $\begingroup$ NASA developed the Common Data Format (.cdf) and it is supported in Mathematica as the "NASACDF" format. However, Mathematica (as of version 11.1.1) only supports version 3.1 and earlier of the NASACDF format. I suggest checking that the version being exported is by Matlab is 3.1 or earlier. You can also be more specific with your Import by specifying the file format; Import["A.cdf", {"NASACDF", "Elements"}]. $\endgroup$
    – Edmund
    Aug 18, 2017 at 18:52

1 Answer 1

2
$\begingroup$

Here are my two cents. But first a disclaimer: I am not very familiar with the NASA CDF and thus I cannot tell if MATLAB or Mathematica do or do not conform to any specific version of the NASA CDF standard. From the info given on the page that was referred by the OP, I would guess that MATLAB follows the standard quite closely. As @Edmund pointed out, Mathematica only supports a rather old version of NASA CDF; maybe this version did not have support for choosing between row majority and column majority. Anyways, MATLAB does provide it now while Mathematica seems to support row majority only.

The following is a very first and premature attempt to cure this shortcoming. The function tries to produce an Association with all the relevant information. (My personal taste is that the Import interface is somewhat old-fashioned and cumbersome, now that associations have been introduced.)

ClearAll[NASACDFImport];
NASACDFImport[file_, OptionsPattern[{
    "Majority" -> "Row"
    }]] := Module[{data, f},
  data = With[{keys = Import[file, {"NASACDF", "Elements"}]},
    AssociationThread[keys, 
     Map[Import[file, {"NASACDF", #}] &, keys]]
    ];
  f = Switch[OptionValue["Majority"],
    "Row", Transpose[ArrayReshape[#, Reverse[Dimensions[#]]]] &,
    "Column", Identity,
    _, Abort[]
    ];
  data[["Datasets"]] = AssociationThread[
    data[["Datasets"]],
    Map[f, data[["Data"]], {2}]
    ];
  KeyDropFrom[data, "Data"];
  data
  ]

In the OP's example, the correct matrix might be returned by

data = NASACDFImport["A.cdf", "Majority" -> "Column"][["Datasets","A",1]]

However, as I do not have the time to test it thoroughly, I do not guarantee for anything. Any suggestions are welcome.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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