Automatic Loading of Import/Export Converters
In addition to the interfaces of Import and Export, Mathematica 8 comes with an autoloading mechanism that simplifies the registration of formats used by the Import and Export framework.
Autoloading versus Manual Loading
Manual Loading
Examples of manually loading converters are given in the tutorials, where the commands to register
are explicitly evaluated.
Autoloading
By placing both the format registration and the converter in appropriate locations, Mathematica 8 can automatically load the format registration. Autoloading of Import and Export converters can be achieved by following these steps:
(In the steps below, you can use $UserBaseDirectory or $BaseDirectory.)
1. If the directory $UserBaseDirectory/SystemFiles/Formats does not exist, create it.
2. Under $UserBaseDirectory/SystemFiles/Formats, create a directory with the name format, where format is the name of the format to be autoloaded.
3. Inside the format directory, place the
code block into a file named
.
For example, in the directory $UserBaseDirectory/SystemFiles/Formats/MyFormat1, the file
would have the following content.
4. Inside the format directory, place the converter source code into a file named
.
For example, in the directory $UserBaseDirectory/SystemFiles/Formats/MyFormat1, the file
would have the following content.
5. With the files in place, when Mathematica starts, $ImportFormats will contain the entry
, and Import[ file, {"MyFormat1", "Header"}] will evaluate appropriately.
In fact, all formats supported by Mathematica are registered using this autoloading mechanism, with the format registration residing in $InstallationDirectory/SystemFiles/Formats.
Furthermore, several formats in Mathematica are made available in source form in the directory $InstallationDirectory/SystemFiles/Formats.
Details of the Registration Mechanism
Directory Name Must Match the Format Name
When using the autoloading mechanism, the folder name must match the format name. Formats can effectively be renamed by using format aliases below.
"Sources" Must Be Explicitly Given If Source Files Are Not in Standard Locations
When using the autoloading mechanism, the Import/Export framework assumes the source file to be located in either $UserBaseDirectory/SystemFiles/Formats/format/Converter.m or $BaseDirectory/SystemFiles/Formats/format/Converter.m. If the source files are located elsewhere, it must be explicitly declared using the
option. For example,
.
Resolving Conflicts
In case of conflicts (for example, the
directory residing in both $InstallationDirectory/SystemFiles/Formats and $UserBaseDirectory/SystemFiles/Formats), the registration in $UserBaseDirectory has highest priority, followed by $BaseDirectory and $InstallationDirectory.
Defining Format Aliases
It is often useful to define format aliases so that you may refer to a format with several different names. Suppose you would like to use
and
as aliases of
. You can do this by creating the file $UserBaseDirectory/SystemFiles/Formats/FormatMappings.m with the following contents.
| In[5]:= |
| Out[5]= | ![]() |
| In[7]:= |
| Out[7]= | ![]() |







