I've developed an importer and exporter for FreeSurfer's MGH/MGZ file format; the specific format isn't important, but I was wondering if there is a 'correct' way to tell Mathematica that file names ending with ".mgh" should be automatically imported using my MGH importer?

Mathematica obviously does this for its built-in importers (e.g., Import["abc.csv"] is equivalent to Import["abc.csv", "CSV"]), so it seems like it should be possible, but I haven't been able to find any information about this in Mathematica's documentation.

One solution, which seems inelegant to me, is this:

Import[name_String /; StringMatchQ[name, __ ~~ ".mgh" ~~ EndOfString]] := 
  Import[name, "MGH"];
Import[name_String /; StringMatchQ[name, __ ~~ ".mgz" ~~ EndOfString]] := 
  Import[name, {"GZip", "MGH"}];

Is there a more principled way of going about this?

  • $\begingroup$ Have you read this tutorial? It details how to develop Import/Export converters. I don't know if it contains the information you're asking for, but it's the best place to look. $\endgroup$ – Szabolcs Jun 20 '14 at 15:41
  • 2
    $\begingroup$ I've read all of those a couple times and they seem to be the only official source of info on getting forms like Import["abc.mgh", "MGH"] to work (using ImportExport`RegisterImporter). Unless I missed something there is no information about the auto-detection of file type in there, however. $\endgroup$ – nben Jun 20 '14 at 15:47

This can now be done using RegisterFormat from the Wolfram Function Repository.


| improve this answer | |

I believe all you need to do is add your extension to the ExtensionMappings.m file in System`ConvertersDump`$FormatsDirectory.

| improve this answer | |
  • $\begingroup$ This does solve the problem locally for my machine; however, the importer is part of a package/library that I would like to distribute, and this solution would require each library user to both have root privilege and go mucking around in their Mathematica systems directories. I'm looking for something that can be done at the time the package is loaded. $\endgroup$ – nben Jun 20 '14 at 15:59
  • $\begingroup$ It's helpful to specify those details :) Your own idea is probably the best option, then, unless you want to do something hacky like Block[{SystemConvertersDumpExtensionMappings=Join[{"*.myFormat"->"MYFORMAT"},Join@@SystemConvertersDumpGetExtensionMappings/@SystemConvertersDump$FormatsDirectory,SystemConvertersDumpExtensionMappings]}, SystemConvertersDumpExtensionMappings] $\endgroup$ – mfvonh Jun 20 '14 at 16:09
  • $\begingroup$ Thanks, that is definitely closer to the kind of answer I was looking for, but it doesn't work. By the way, AppendTo[SystemConvertersDumpExtensionMappings, "*.mgh" -> "MGH"] would seem to be sufficient, but either way that I do it, Import fails to infer the format of files ending with the extension. Also, how can I find more documentation on these namespaces? As far as I can tell these are not documented anywhere... $\endgroup$ – nben Jun 20 '14 at 16:27
  • 1
    $\begingroup$ Not documenting things is one of Mathematica's strongest features :P add //Trace to your import line and you can see what it does. Beware though, it's a lot of output. Usually easier to export that text and view it in a text editor. $\endgroup$ – mfvonh Jun 20 '14 at 17:02
  • 1
    $\begingroup$ Use double backticks to delimit inline code that contains backticks. (See how I edited.) $\endgroup$ – Szabolcs Jun 20 '14 at 17:13

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.