1
$\begingroup$

I had a .mat data that I wanted to import to Mathematica. I tried using

Import["File path","Data"]

But it didn't work. It just returned de message "Import:Cannot import data as MAT format."

Afterwords, I discovered that it wasn't working because the file contained NaNs (I removed the NaNs form the original file and tried to import it and it worked).

I wanted to know why Mathematica couldn't just import it normally and how to import this kind of data (.mat with NaNs) without having to change the actual file.

$\endgroup$
1
  • $\begingroup$ E.g. import it as as String. Delete NaNs. Import the string ("ImportString"). $\endgroup$ Apr 5 at 14:41
2
$\begingroup$

Wolfram Language has its own symbolic representation of special numeric values (e.g. Indetereminate or Infinity) but it cannot handle very well the exceptional floating point values which can be smuggled into the system via LibraryLink inside packed arrays or NumericArrays. As a remedy, starting from WL 12.2, LibraryLink makes a pass over every packed/numeric array which comes from outside to check for nans and infs and it does not let them through. This in turn affects the Import of all formats which are implemented with LibraryLink and MAT is one of them.

To get the old behavior back, one can set the following system option:

SetSystemOptions["LibraryLinkOptions" -> "TestFloatingPointExceptions" -> False]

For MAT Import specifically, there is an alternative in the form of "IndeterminateValue" option:

Import["file.mat", "Data", "IndeterminateValue" -> 42.24]

will replace all indeterminate values with 42.24. You can also set different replacements for different kinds of special values like this:

 Import["file.mat", "Data", "IndeterminateValue" -> <|Infinity -> 1000.0, -Infinity -> -1000.0, Indeterminate -> 0|>]
$\endgroup$

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.