# Reading KEA files in MATLAB

The KEA file format is built on HDF5 so anything which can read HDF5 can read a KEA file, including MATLAB. I’m not quite sure why you’d want to use MATLAB instead of using Python, in particular as you can read KEA files and many other formats through GDAL. However, I’ve been claiming it’s possible to use KEA with MATLAB for a while so I though it would be a good idea to post an example!

This example uses CASI data over Injune, which is part of the test dataset for RSGISLib, you can download from here.

1. Get file information

```info = h5info('injune_p142_casi_sub_ll.kea');
% Get bandnames
info.Groups.Name
```

If you don’t care about the geospatial information you can skip this step.

```% Read in resolution
xRes = res(1);
yRes = res(2);

% Top left coordinate
topLeftX = topLeft(1); % Longitude
topLeftY = topLeft(2); % Latitude

% Size
imageSizeX = imageSize(1);
imageSizeY = imageSize(2);

% Calculate bottom right
bottomRightX = topLeftX + (double(imageSizeX) * xRes);
bottomRightY = topLeftY + (double(imageSizeY) * yRes);

lon_axis = linspace(topLeftX,bottomRightX,imageSizeX);
lat_axis = linspace(bottomRightY,topLeftY,imageSizeY);
```

This assumes reading in bands 12, 8 and 3.

```band12 = transpose(h5read('injune_p142_casi_sub_ll.kea','/BAND12/DATA'));
```

4. Display data
A three band composite is created, this is stretched and then displayed.

```% Create composite
composite = cat(3,band12,band8,band3);

% Apply linear stretch