Saving CLEA data to text file

Project CLEA only provides a way to make measurements, but it won't do the calculation for you. And in this LSS lab, you have a huge amount of objects to calculate. Therefore we want to use MATLAB to save our measurements, do the calculation (for all objects in the same time!), and write these data to a file.


Saving your measurements

To simplify, assuming we only have 3 objects. For each of them we are going to measure the wavelengths for K line and H line.

Let's use the first rows of waveK and waveH to save our wavelength measurements of object 1:

waveK(1) = 4030;
waveH(1) = 4060;

Then use the second rows for object 2:

waveK(2) = 4080;
waveH(2) = 4100;

and so on. Then, use the vector operations, we have the velocity from both K- and H-line (note that we only want the absolute value):

velK = ... ;
velH = ... ;

Taking the average of these two gives us the average velocity:

avgV = 0.5*(velK + velH);

Now we have five 1x3-size arrays that we want to save. (In your case, they are 1x(# of objects)-size arrays!)

Creating cell array for object IDs

To save the object names, we need to use Cell Array. A cell array is a data type with indexed data containers called cells. Each cell can contain any type of data. Cell arrays commonly contain lists of text strings, combinations of text and numbers from spreadsheets or text files, or numeric arrays of different sizes. Since the object names may not have same length, we must put each name into a cell so that they have the same size (1x1, because it's one cell).

We can create a cell array by using the {} operator:

objNames = {'object1'; 'object10'; 'object100'}
objNames = 


This is different from using the matrix constructor operator []. You can try substituting the {} in the command above by [] and see what will happen. (Basically you'll get an error message telling you that these arguments don't have the same dimension, and you can't construct a matrix using these elements.)

Also note that, when accessing data in a cell array, indices in smooth parentheses () refers to the cell:

ans = 


but if you enclose indices in curly braces {}, it refers to the contents within individual cells:

ans =


This is something you should keep in mind, because you can't write a cell to file, but you can write the content of that cell to file!

Writing data to file -- 'fprintf'

To write both the object names (cells) and the measured data (numbers) to file, we have to use the C-like command fprintf. This allows for arbitrary output formats, and it is infinitely flexible.

First, we must open a file (e.g. mydata.txt) for writing. To do that we need a File Identifier, here we called it fout:

fout = fopen('mydata.txt','w');

The permission argument 'w' will discard any existing contents in the file mydata.txt. To append data to the end of an already-existing file, use 'a' instead.

The syntax of fprintf can be found by using help:

help fprintf
 FPRINTF Write formatted data to file.
    COUNT = FPRINTF(FID,FORMAT,A,...) formats the data in the real
    part of array A (and in any additional array arguments), under
    control of the specified FORMAT string, and writes it to the file
    associated with file identifier FID.  COUNT is the number of bytes
    successfully written. FID is an integer file identifier obtained
    from FOPEN. It can also be 1 for standard output (the screen) or 2
    for standard error. If FID is omitted, output goes to the screen.

    ... ...

Because we have more than one object (i.e. more than one row) and we want to separate the measurements for different object into rows that have corresponding object ID, let's use the for loop we learned last week: (Note that the nunmber of times for the loop to run is decided by the argument i=1:3, which should be i=1:(# of your objects) in your lab)

for i=1:3
    fprintf(fout,'%s,%E,%E,%E,%E,%E\n', objNames{i}, avgV(i), waveK(i), velK(i), waveH(i), velH(i));

(Note the {} for objNames and () for others!)

The conversion character %s stands for strings, and %E means exponential notation, using a uppercase E. Other choices are %d (integers), %c (characters), etc. And the character \n is an escape character sequence we use to specify nonprinting characters in a format specification, means new line. You can use the MATLAB Product Help to search for more specifiers.

After we finished writing, don't forget to use fclose and the corresponding file identifier to close the file:


To check the file you just generated, the command type can display the contents of file:

type mydata.txt