Writing numeric data to text file
The simplest format for data is text files. These files are frequently also called ASCII files (ASCII -- pronounced aski -- stands for American Standard Code for Information Interchange, a standard for numerically representing alphabets developed in the 1960s). There are more than one way to save data to a text file, depends on what kind of data we have, and what kind of output format we want.
(This tutorial is not required for this lab, since you need to save the object ID also. But this might be useful for you in the future.)
Basically, function save saves workspace variables to file (but only numeric variables!). For example, we have the 1x3 double arrays waveK and velK, and we want to save them into a file data.txt. The syntax is simple:
save data.txt waveK velK -ascii
but don't forget to include -ascii as the format argument, which will save the numbers in 8-digit ASCII format.
This file looks like this:
4.0300000e+03 4.0800000e+03 3.9000000e+03 7.3456370e+03 1.1158858e+04 2.5687384e+03
which has waveK and velK in rows, not columns; normally we would like each row to represent each object.
Therefore, we need to combine our data to a matrix (and do transpose if necessary) before we save them to file. Recall your knowledge of arrays and vectors, and make the combined data array (note the transpose!):
m = [avgV; waveK; velK; waveH; velH]'
m = 1.0e+04 * 0.7132 0.4030 0.7346 0.4060 0.6918 1.0550 0.4080 1.1159 0.4100 0.9941 0.2097 0.3900 0.2569 0.3990 0.1626
Then we save the data: (let's use the same file name to show you that unless you include the argument -append, the original content is erased everytime you use save)
save data.txt m -ascii type data.txt
7.1316134e+03 4.0300000e+03 7.3456370e+03 4.0600000e+03 6.9175899e+03 1.0550133e+04 4.0800000e+03 1.1158858e+04 4.1000000e+03 9.9414085e+03 2.0973228e+03 3.9000000e+03 2.5687384e+03 3.9900000e+03 1.6259073e+03
Though save is the simplest way to save numeric data, it's less flexible. One example is that save can only generate tab-delimited file. However, for CLEA to read, we need comma-separated file, which is not supported by save. An alternative way is to use dlmwrite (try typing help dlmwrite in the Command Line to find the syntax):
dlmwrite('data2.txt', m, 'delimiter', ',', 'precision', '%E')
The attribute delimiter and the following value (',' here) defines the delimiter string to be used in separating matrix elements, and precision gives the numeric precision, which can be specified by the number of siginificant digits or a C-style format string starting in %, such as %10.5f. The conversion character %E we used here means exponential notation, using a uppercase E as in 3.1415E+00.
Now we have the comma-separated file:
7.131613E+03,4.030000E+03,7.345637E+03,4.060000E+03,6.917590E+03 1.055013E+04,4.080000E+03,1.115886E+04,4.100000E+03,9.941408E+03 2.097323E+03,3.900000E+03,2.568738E+03,3.990000E+03,1.625907E+03
Other possible values of delimiter include ' ' (space) or '\t' (tab).