# The Copenhagen Chemometrics Group

## 1. Handling multi-way data in MATLAB

**Contents**

- Load data
- Plot data
- Manage data
- Summary

**Data used**

claus.mat contains fluorescence excitation emission data from five samples containing tryptophan, phenylalanine, and tyrosine.

**Purpose **

Learning about the Multi-way Toolbox for MATLAB and about how to view, arrange, and plot multi-way data in MATLAB.

## 1. Load data

Use `load claus`

and find out what the file contains (use `whos`

).

### 2. **Plot data**

Use the plot functions to view data and learn about the three-way structure of the data and how to access it in MATLAB. If you are low on (computer!) memory go to step three initially. Do the following plots and *understand *what is shown in the plots! Type help subplot to learn of the function subplot.

**Plotting Emission spectra **`subplot(2,1,1),plot(X(:,:,1)') `

subplot(2,1,2),plot([X(:,:,1) X(:,:,2)]')

What’s the difference between the two plots?

*Plotting excitation spectra*

Try to plot some excitation spectra as

`plot(X(:,1,:)')`

This does not work! The reason is that `X(:,1,:)`

is not a matrix. Use the command size to figure out what `X(:,1,:)`

is (Tell me)? With the command plot, we can only plot matrices so we have modify things a bit

`subplot(2,1,1),plot(squeeze(X(:,1,:))') `

subplot(2,1,2),plot(squeeze(X(:,30,:))')

What’s on the plots. What’s the difference between the two plots?

If we want to be fancy we can add correct axes and titles to the plots. In the vector ExAx (type `whos`

to see the data available), the actual 61 excitation wavelengths are given.

We can add these to the plots by

`subplot(2,1,1),plot(ExAx,squeeze(X(:,1,:))')`

And we can add more by typing

`xlabel('Wavelength/nm')`

`ylabel('Intensity')`

title('Fluorescence excitation spectra',

`'FontWeight','Bold','FontSize',14)`

**Plotting landscapes**

All the following commands will plot all the data from sample number 2. Try to understand the differences in the plots. That is important when you want to visualize your own data. Use MATLAB help for functions you don’t understand.

`clf`

subplot(3,2,1),plot(squeeze(X(2,:,:)))

subplot(3,2,2),plot(reshape(X(2,:),201,61))

subplot(3,2,3),plot(reshape(X(2,:),201,61)')

subplot(3,2,4),mesh(reshape(X(2,:),201,61))

subplot(3,2,5),mesh(reshape(X(2,:),201,61)')

subplot(3,2,6),mesh(EmAx,ExAx,reshape(X(2,:),201,61)')

axis tight

All plots show the same data. What’s the difference?

### 3. **Manage data**

As an example of managing the data, reduce the size of **X **for easier computations. Use only every third wavelength in the excitation mode (mode three) and every sixth wavelength in the emission mode (mode two). An easy way to do that is the following

`Xnew = X(:,1:6:end,1:3:end);`

If you have very little memory reduce the data even more. Remember to reduce EmAx and ExAx accordingly if you plan to use those for plotting.

### 4. Summary

In this chapter it has been shown how to manipulate and plot the data.