mat
The DolphinDB mat plugin can be used to read data from a .mat file to DolphinDB or the other way around. The data types are automatically converted during the writing. The plugin is developed based on the MATLAB Runtime library.
1. Installation (with installPlugin
)
Required server version: DolphinDB 2.00.10 or higher
Supported OS: Linux x86-64 and Linux JIT
Prerequisites:
(1) Download and install the MATLAB Runtime Installer. Execute the following commands in the Linux terminal:
wget <https://ssd.mathworks.com/supportfiles/downloads/R2016a/deployment_files/R2016a/installers/glnxa64/MCR_R2016a_glnxa64_installer.zip>
unzip MCR_R2016a_glnxa64_installer.zip -d matlabFile
cd matlabFile
./install -mode silent -agreeToLicense yes -destinationFolder /home/Matlab
(2) Before starting the DolphinDB service, execute the following command:
export LD_LIBRARY_PATH=/home/Matlab/v901/bin/glnxa64:$LD_LIBRARY_PATH
Installation Steps:
(1) Use listRemotePlugins to check plugin information in the plugin repository.
Note: For plugins not included in the provided list, you can install through precompiled binaries or compile from source. These files can be accessed from our GitHub repository by switching to the appropriate version branch.
login("admin", "123456")
listRemotePlugins()
(2) Invoke installPlugin for plugin installation
installPlugin("mat")
(3) Use loadPlugin to load the plugin before using the plugin methods.
loadPlugin("mat")
2. Method References
extractMatSchema
Syntax
extractMatSchema(file)
Details
Generate the schema of the data set in the .mat file. It contains 2 columns: column name and data type.
Parameters
- file: A STRING scalar indicating the absolute path where the .mat file is located.
Example
schema = mat::extractMatSchema("<FileDir>/simple.mat");
loadMat
Syntax
loadMat(file, [schema])
Details
Read a .mat file and return a dictionary.
- key: A string indicating the variable name.
- value: A matrix or vector containing the data corresponding to the specified variable. When converting a CHAR array, it returns a STRING vector.
Parameters
- file: A STRING scalar indicating the absolute path where the .mat file is located.
- schema: A table containing the names and data types of columns, which is used to specify the data type of each column. You can modify the data type as appropriate.
Examples
schema = mat::extractMatSchema("<FileDir>/simple.mat");
ret = mat::loadMat("<FileDir>/simple.mat",schema);
convertToDatetime
Syntax
convertToDatetime(data)
Details
Convert the DOUBLE temporal variables in the .mat file to DolphinDB DATETIME values.
Parameters
- data: A DOUBLE scalar/vector/matrix indicating the variable to be converted.
Examples
schema=mat::extractMatSchema("<FileDir>/simple.mat");
ret=loadMat("<FileDir>/simple.mat",schema);
//t_num in simple.nat is a temporal variable of double type
ret=mat::convertToDatetime(ret[`t_num]);
writeMat
Syntax
writeMat(file, varName, data)
Details
Write a matrix to a .mat file.
Parameters
- file: A STRING scalar indicating the file name to which the matrix is written.
- varName: A STRING scalar indicating the variable name corresponding to the data after being written.
- data: A BOOL/CHAR/SHORT/INT/LONG/FLOAT/DOUBLE matrix to be written to the .mat file.
Examples
data = matrix([1, 1, 1], [2, 2, 2]).float()
mat::writeMat("var.mat", "var1", data)
Data Type Mappings
Integer
MATLAB type | DolphinDB type |
---|---|
int8 | CHAR |
uint8 | SHORT |
int16 | SHORT |
uint16 | INT |
int32 | INT |
uint32 | LONG |
int64 | LONG |
uint64 | not supported |
- All data types in DolphinDB are signed types. To avoid data type overflow, unsigned types are converted to higher-level signed types. For example, unsigned CHAR is converted to signed SHORT, unsigned SHORT is converted to signed INT, etc. 64-bit unsigned types cannot be converted.
- Unsigned long long types are not supported in DolphinDB. If the data type in a .mat file is bigint unsigned, you can specify schema of method
loadMat
as DOUBLE or FLOAT. - The smallest value of integral types in DolphinDB is NULL, including -128 (CHAR), -32,768 (SHORT), -2,147,483,648 (INT) and -9,223,372,036,854,775,808 (LONG).
- NaN and Inf values in MATLAB are converted to DolphinDB NULL values.
Floating-point
MATLAB type | DolphinDB type |
---|---|
single | FLOAT |
double | DOUBLE |
String
MATLAB type | DolphinDB type |
---|---|
character array | STRING |