| Contents | Index |
This topic shows how to create a standalone program, matimport, to copy data from an external source into a MAT-file. The format of the data is custom, that is, it is not one of the file formats supported by MATLAB.
The matimport.c example:
Creates variables to read the external data.
Copies the data into mxArray variables.
Assigns a variable name to each mxArray. This is the variable name to use in the MATLAB workspace.
Writes the mxArray variables and associated variable names to the MAT-file.
To use the data in MATLAB:
Build the standalone program matimport.
Run matimport to create the MAT-file matimport.mat.
Open MATLAB.
Use one of the techniques described in Importing MAT-Files.
The following topics describe these steps in detail. To see the code, open the file in the MATLAB Editor. The C statements in these topics are code snippets shown to illustrate a task. The statements in the topics are not necessarily sequential in the source file.
There are two external data values, a string and an array of type double. The following table shows the relationship between the variables in this example.
| External Data | Variable to Read External Data | mxArray Variable | MATLAB Variable Name |
|---|---|---|---|
| Array of type double | extData | pVarNum | inputArray |
| String | extString | pVarChar | titleString |
The following statements declare the type and size for variables extString and extData:
#define BUFSIZE 256 char extString[BUFSIZE]; double extData[9];
Use these variables to read values from a file or a subroutine available from your product. This example uses initialization to create the external data:
const char *extString = "Data from External Device";
double extData[9] = { 1.0, 4.0, 7.0, 2.0, 5.0, 8.0, 3.0, 6.0, 9.0 };
The MAT-File Library uses pointers of type mxArray to reference MATLAB data. The following statements declare pVarNum and pVarChar as pointers to an array of any size or type:
/*Pointer to the mxArray to read variable extData */ mxArray *pVarNum; /*Pointer to the mxArray to read variable extString */ mxArray *pVarChar;
To create a variable of the proper size and type, select one of the mxCreate* functions from the MX Matrix Library.
The size of extData is 9, which the example copies into a 3-by-3 matrix. Use the mxCreateDoubleMatrix function to create a two-dimensional, double-precision, floating-point mxArray initialized to 0.
pVarNum = mxCreateDoubleMatrix(3,3,mxREAL);
Use the mxCreateString function to create an mxArray variable for extString:
pVarChar = mxCreateString(extString);
matimport.c assigns variable names inputArray and titleString to the mxArray data. Use these names in the MATLAB workspace. For more information, see View the Contents of a MAT-File.
const char *myDouble = "inputArray"; const char *myString = "titleString";
Copy data from the external source into each mxArray.
The C memcpy function copies blocks of memory. This function requires pointers to the variables extData and pVarNum. The pointer to extData is (void *)extData. To get a pointer to pVarNum, use one of the mxGet* functions from the MX Matrix Library. Since the data contains only real values of type double, this example uses the mxGetPr function:
memcpy((void *)(mxGetPr(pVarNum)), (void *)extData, sizeof(extData));
The following statement initializes the pVarChar variable with the contents of extString:
pVarChar = mxCreateString(extString);
Variables pVarNum and pVarChar now contain the external data.
The matOpen function creates a handle to a file of type MATFile. The following statements create a file pointer pmat, name the file matimport.mat, and open it for writing:
MATFile *pmat; const char *myFile = "matimport.mat"; pmat = matOpen(myFile, "w");
The matPutVariable function writes the mxArray and variable name into the file:
status = matPutVariable(pmat, myDouble, pVarNum); status = matPutVariable(pmat, myString, pVarChar);
To close the file:
matClose(pmat);
To free memory:
mxDestroyArray(pVarNum); mxDestroyArray(pVarChar);
To build the application, use the mex function with the appropriate MAT options file. For more information, see Compiling and Linking MAT-File Programs.
For example, to use the Microsoft Visual C++® Version 8.0 compiler, select it as described in Selecting a Compiler on Windows Platforms. The options file for this compiler is msvc80engmatopts.bat. Use the following MATLAB commands to build matimport:
% Create full path name for options file
optionsfile = fullfile(matlabroot, ...
'bin', 'win32', 'mexopts', 'msvc80engmatopts.bat');
mex('-v', '-f', optionsfile, 'matimport.c')
Run matimport to create the file matimport.mat. Either invoke the program from the system command prompt, or at the MATLAB command prompt, type:
!matimport
Any user with a compatible version of MATLAB can read the matimport.mat file. Start MATLAB and use the load command to import the data into the workspace:
load matimport.mat
To see the variables, type whos; MATLAB displays:
Name Size Bytes Class inputArray 3x3 72 double titleString 1x43 86 char
![]() | Custom Applications to Read and Write MAT-Files | Examples of MAT-File Applications | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |