Brereton Chemometrics
.pdfAPPENDICES |
473 |
|
|
Figure A.46
Scatterplot in Matlab
identical number of elements. There are various ways of telling Matlab that a variable is a string (or character) rather than numeric variable. Any data surrounding by single quotes is treated as a string, so the array c = [‘a’; ‘b’; ‘c’] will be treated by Matlab as a 3 × 1 character array. Figure A.47 illustrates the use of this method. Note that in order to prevent the labels from overlapping with the points in the graph, leaving one or two spaces before the actual text helps. It is possible to move the labels later in the graph editor if there is still some overlap.
Sometimes the labels are originally in a numerical format, for example they may consist of points in time or wavelengths. For Matlab to recognise this, the numbers can be converted to strings using the num2str function. An example is given in Figure A.48, where the first column of the matrix consists of the numbers 10, 15 and 20 which may represent times, the aim being to plot the second against the third column and use the first for labelling. Of course, any array can contain the labels.
A.5.7.5 Three-dimensional Graphics
Matlab can be very useful for the representation of data in three dimensions, in contrast to Excel where there are no straightforward 3D functions. In Chapter 6 we used 3D scores and loadings plots.
Consider a scores matrix of dimensions 36 × 3 (T) and a loadings matrix of dimensions 3 × 25 (P). The command plot3(T(:,1),T(:,2),T(:,3)) produces a graph of all three columns against one another; see Figure A.49. Often the default
474 |
CHEMOMETRICS |
|
|
Figure A.47
Use of text command in Matlab
orientation is not the most informative for our purposes, and we may wish to change this. There are a huge number of commands in Matlab to do this, which is a big bonus for the enthusiast, but for the first time user the easiest is to select the right-hand rotation icon, and interactively change the view; see Figure 4.50. If that is the desired view, leave go of the icon.
Often we want to return to the view, and a way of keeping the same perspective is via the view command. Typing A = view will keep this information in a 4 × 4 matrix A. Enthusiasts will be able to interpret these in fundamental terms, but it is
APPENDICES |
475 |
|
|
Figure A.48
Using numerical to character conversion for labelling of graphs
not necessary to understand this when first using 3D graphics in Matlab. However, in chemometrics we often wish to look simultaneously at 3D scores and loadings plots and it is important that both have identical orientations. The way to do this is to ensure that the loadings have the same orientation as the scores. The commands
figure(2)
plot3(P(:,1),P(:,2),P(:,3))
view(A)
should place a loadings plot with the same orientation in Figure 2. Sometimes this does not always work the first time; the reasons are rather complicated and depend on
476 |
CHEMOMETRICS |
|
|
Figure A.49
A 3D scores plot
Figure A.50
Using the rotation icon
APPENDICES |
477 |
|
|
Figure A.51
Scores and loadings plots with identical orientations
478 |
CHEMOMETRICS |
|
|
the overall starting orientation, but it is usually easy to see when it has succeeded. If you are in a mess, start again from scratch. Scores and loadings plots with the same orientation are presented in Figure A.51.
The experienced user can improve these graphs just as the 2D graphs, for example by labelling axes or individual points, using symbols in addition to or as an alternative to joining using a line. The scatter3 statement has similar properties to plot3.