To start scripting an object:
• Click once on the component to select it.
• Click on the component with the right mouse button.
• Choose Edit Script from the context menu.
You’ll see a new window called the Script Editor containing three subroutine stubs in which you insert your own scripting code. Figure 19-6 shows a portion of the completed VB Script script for an object called SmileyEvent.
The script you write will usually contain at a minimum the following three subroutines:
• A starting routine, called once when execution of the component begins. This is a good place to initialize variables, open files for reading and writing, etc.
• An execution routine that by default takes as arguments the collections Inputs and Outputs.
• A stopping routine, called once when execution of the component stops.
The commands in each section are executed in sequence whenever the Mathcad worksheet is calculated. What you will include in these subroutines is determined largely by the properties of the OLE object you are scripting; consult the documentation for the server or control.
Choose Close & Return from the Script Editor’s File menu when you have completed your script and want to resume working in the Mathcad worksheet.
Functions for reading and writing ASCII data files
If the data file you want to import or export to is a plain ASCII file, you can use components for importing and exporting data as described in the first three sections of this chapter. Alternatively, you can use Mathcad’s built-in functions for reading plain ASCII data files. These built-in functions are provided mainly for compatibility with worksheets created in earlier versions of Mathcad, but you can use them in a Mathcad 7 worksheet if you like
Files in plain ASCII format consist only of numbers separated by commas, spaces, or carriage returns. The following are some examples of files that the built-in functions can read, assuming the data are in ASCII format:
• A file containing experimental data captured with data-acquisition hardware and software.
• A file created by printing from a spreadsheet program to the disk.
• A column of numbers typed into a word processor and saved in plain ASCII format.
• Output from a BASIC program.
• Data downloaded from a mainframe database.
Arguments to file access functions
The argument you supply to any Mathcad file access function described in this section is a string expression-or a variable to which a string is assigned-that corresponds either to:
• the name of a data file in the working directory of the Mathcad worksheet you’re currently working on; or
• a full or relative path to a data file located elsewhere on a local or network file system.
As described in “Variables and Constants,” a string in Mathcad is a special kind of math expression that always appears between double quotes, and you start a string expression in an empty math placeholder by typing the double-quote (,,) key.
If the data file you want to use is located in the working directory of the current Mathcad worksheet, the string expression you supply to a file access function can simply contain the name of the file you are reading from or writing to. For example, the string expression” data2 .prn” (quotes included) refers to the file data2 .prn located in the working directory of the current Mathcad worksheet. Any worksheet containing such string expressions in file access functions can be safely moved to another file system with a different directory structure, provided that you move any required data files along with your worksheet.
See the section “Strings” in Chapter 8 for more information about creating and editing strings. For examples of how to use string expressions as arguments to data file access functions, see Figure 19-7 though Figure 19-13
ASCII data file access functions
Mathcad includes six functions for accessing numerical data stored in ASCII data files: READ, WRITE, APPEND, READPRN, WRITEPRN, and APPENDPRN. They each take as an argument a string expression as described above.
These functions have the following properties
• You must type the function name all in uppercase. Alternatively, you may insert the function into your document by choosing Function from the Insert menu and double-clicking on the function name.
• If Mathcad cannot find a data file, it marks the file-access function with an error message indicating that it could not find the file you specified. If Mathcad tries to read a file and the format is incorrect, it marks the function with an error message.
• The WRITE, APPEND, WRITEPRN, and APPENDPRN functions must appear alone on the left side of a definition.
• Each new equation reopens the data file. When you read data, for example, each new equation starts reading at the beginning of the file.
The functions READ, WRITE and APPEND can be used with range variables. The remaining functions never use range variables.
Reading and writing data in unstructured files
This section discusses how to use READ, WRITE, and APPEND to read and write data in unstructured files: files containing numbers, but not necessarily in rows and columns.
Figure 19-7 shows two ways to use READ to read data from a file
The first READ equation defines N as the first value in the data file s i z e s •da t. The second READ equation fills the array y with the first 100 numbers in the data file papageno. da t located in the root directory
When Mathcad writes data to a file, it separates successive numbers with spaces.
Mathcad also inserts line breaks to keep the lines shorter than 80 characters. When you use WRITE, all values are saved to the file with maximum precision, regardless of the global format of the document.
Mathcad ignores units when it writes data to a data file.
Like the READ function, the WRITE function reopens the file and starts at the beginning for each new equation. If you want to write data to a file from several different equations, use the APPEND function instead of the WRITE function in the second and subsequent equations.
Warning: If you use the WRITE function on the same file in two separate equations, the data from the second equation will overwrite the data from the first equation
Reading and writing data in structured files
This section discusses how to use READPRN, WRITEPRN, and APPENDPRN to read and write data in structured files. A structured data file is a data file with a fixed number of values per line. For example, if you print a rectangular area from a spreadsheet into a file, the resulting rows and columns of numbers will be a structured file.
Suppose you have an ASCII text file containing the data shown below. These numbers could come from a spreadsheet or from any other source.
The READPRN function reads the entire data file, determines the number of rows and columns, and creates a matrix out of the data
When Mathcad reads data with the READPRN function:
• Each instance of the READPRN function reads an entire data file.
• All lines in the data file must have the same number of values. (Lines containing no values are ignored.) If the lines in the file have differing numbers of values, Mathcad marks the READPRN equation with an error message.
• The READPRN function ignores text in the data file.
• The result of reading the data file is an m-by-n matrix, where m is the number of lines containing data in the file and n is the number of values per line. To define a matrix out of the numbers in a data file, type an equation like M := READPRN( ” file. prn “). Do not use subscripts on M. READPRN returns a matrix, so no subscripts are necessary.
Unlike the WRITE function, the WRITEPRNfunction writes out the data in columns. The precision of the data written out and column widths are controlled by PRNPRECISION and PRNCOLWIDTH which you can set by choosing Options from the Math menu and clicking on the Built-in Variables tab. In the example above, note that since the PRNPRECISION is set to four, the numbers are written to four decimal places. Since PRNCOLWIDTH is set to eight, each column has space for eight characters. Since PRNPRECISJON and PRNCOLWIDTH can be varied independently, you must take care to choose them in such a way that the column width can accommodate all the digits, together with a space to separate the columns.
If the array you are writing is either a nested array (an array whose elements are themselves arrays) or a complex array (an array whose elements are complex) then WRITEPRN will not create a simple ASCII file. Instead, WRITEPRN creates a file using a special format unlikely to be readable by other applications. This file can, however, be read by Mathcad’s READPRN function.