How to import Avro files into MATLAB
Avro files can be read directly in MATLAB using the Java library distributed by Mathworks.
Please note that the latest version matlab-avro-interface returns some errors while parsing the schema.
This guide to correctly open Avro files in MATLAB only works with matlab-avro-interface version 0.8.3
Requirements
- Java JDK8 or higher
- Maven
- Follow the instructions in the README.txt file to install it
- Matlab-Avro interface v0.8.3
Version 0.8.3 can be downloaded directly from GitHub, or cloned by running the following command in the terminal:
git clone -b v0.8.3 --recursive https://github.com/mathworks-ref-arch/matlab-avro
Post-Installation patch
To set up the library to read Empatica's Avro files, the Avro version in the pom file needs to be updated with the following steps:
- Navigate to the repository folders and identify the pom.xml file. You should find it inside matlab-avro/Software/Java path
- Open pom.xml and change the value of avro.version to 1.11.0 (line 16)
Open a terminal in the folder where pom.xml is stored and run:
mvn dependency:copy- Once it has finished, the message “Build success” should appear
Configure Matlab
If you want to install the matlab-avro library only temporarily (you have to repeat this procedure every time you re-open MATLAB) simply open MATLAB and run the startup.m script, found in matlab-avro/Software/MATLAB.
If you want to install the library permanently follow these steps:
- If you don’t have it yet, create a file named javaclasspath.txt in your prefdir folder. To know your prefidir folder, run the command prefdir in MATLAB
- Open javaclasspath.txt and write inside the path to the .jar file of the matlab-avro library (located in .../matlab-avro/Software/MATLAB/lib/jar). Then save the file.
Your javaclasspath.txt should look like: .../matlab-avro/Software/MATLAB/lib/jar/matlabavro-0.3.jar - Add the matlab-avro/Software/MATLAB/app/system folder to your MATLAB path (From MATLAB Home > Set Path > add the folder and save) and then restart MATLAB
The Java library needs to be configured either per-instance, running the startup.m script with each MATLAB instance - or permanently, by configuring javaclasspath.txt
Read an Avro file
To read Empatica’s Avro files, run the following two commands:
myReader = matlabavro.DataFileReader(PATH_TO_AN_AVRO_FILE); myReaderData = myReader.next();myReaderData will be a MATLAB struct containing the content of the parsed Avro file.
(replace PATH_TO_AN_AVRO_FILE with the path of an Avro file)