Tutorial > Printing and Exporting Data > Lesson 2: Simple Output Examples
  

Lesson 2: Simple Output Examples
This section provides a “birds-eye view” of how PV-WAVE I/O (Input/Output) works by providing some examples showing how data is transferred in to and out of variables.
In this lesson, you will learn how to do the following:
* Transfer Data from a Variable to a File
* Print Values to a File
* Change Data in an Existing File
Transfer Data from a Variable to a File
PV-WAVE variables point to portions of memory that are set aside during a session to store data. The first step in analyzing data is usually to transfer it into PV-WAVE variables.
To transfer data from a variable to a file, do the following:
1. Define ylow to be a vector of integers. This is a simple data set.
ylow = [77, 63, 42, 56]
2. DC_WRITE_FREE handles the opening and closing of the file. It takes the values in ylow and stores them in a file named var2data.dat. Because the Column keyword was used, each value is written on different lines. The returned value status can be checked to see if the process completed successfully.
status = DC_WRITE_FREE('var2data.dat', ylow, /Column)
With DC_WRITE_FREE you do not need to explicitly close the file, or use the file’s LUN.
Print Values to a File
In this example, you will write data to a file using the PRINTF command.
To print values to a file, do the following:
1. Open the file print2data.dat for writing.
OPENW, 2, 'print2data.dat'
2. Write the values to the file, each value on a new line.
PRINTF, 2, '77'
PRINTF, 2, '63'
PRINTF, 2, '42'
PRINTF, 2, '56'
3. Close the file.
CLOSE, 2
Change Data in an Existing File
In this example, you will change the value on the third line of an existing file. For this example, the file created in Print Values to a File is used.
To change the third data point in print2data.dat, do the following:
1. Open the file print2data.dat for updating.
OPENU, 1, 'print2data.dat'
2. Set the file pointer to the third line in the file. The position of the file pointer is a byte offset from the start of the file, in this case 6 bytes.
POINT_LUN, 1, 6
3. Replace the value 42 with the new value 89.
PRINTF, 1, '89'
4. Close the file.
CLOSE, 1
5. Now the contents of data3.dat look like:
77
63
89
56

Version 2017.1
Copyright © 2019, Rogue Wave Software, Inc. All Rights Reserved.