SMathStudio.0_85.IOFiles.Article.Eng(1).pdf

(453 KB) Pobierz
Input/Output of data and file manipulation in SMath Studio
Prepared by Gilberto E. Urroz, September 2009
SMath Studio provides functions wfile , rfile, and dfile for output into a file, input from a file, and
deleting an existing file. To illustrate the use of these functions we could start by writing some data to
a file and exploring the contents of the file. Before we do that, however, I want to address an issue
related to the location of the SMath Studio installation folder.
The location of the SMath Studio installation folder
When I first attempted this after installing SMath Studio in my Windows Vista Ultimate 64-bit machine
I kept getting a message indicating that access to the file location was not permitted. This was so
because SMath Studio , by default, sends output to and reads input from the folder /*SMath Studio
Installation Folder*/user/ . In my computer, the *SMath Studio Installation Folder* corresponds to the
directory C:\Program Files(x86)\SMath\SMath Studio\ and it requires Administrator access to modify
its contents. To be able to write data from and read data into SMath Studio , therefore, I copied the
folder C:\Program Files(x86)\SMath\ to a different location. In my case the location chosen was:
C:\Users\Gilberto E. Urroz\Documents\NUMERICAL_APPLICATIONS\SMath
In your computer it could be any location where you don't need Administrator rights to modify content.
I then proceeded to create the folder user , where the data files would reside. Thus, the full address of
this new folder, in my case, is:
C:\Users\Gilberto E. Urroz\Documents\NUMERICAL_APPLICATIONS\SMath\SMath Studio\user
To facilitate access to the program, at this point, I created a new shortcut to SMath Studio in my
desktop. Now we proceed to illustrate the use of the wfile command.
Writing data to a file
You can use function wfile to write a single item of data into a file. Consider the following examples 1 :
You can open the files using, for example, Notepad in a Windows system to check the contents of the
file:
1 I recommend you use only lowercase letters in the filenames used to store data. I had difficulties getting SMath Studio
to read data out of files with uppercase letters in their names.
Page 1 © 2009 – Gilberto E. Urroz
988531807.011.png
Thus, you'll need to use the numerical result that you want to write out to a file as the first argument of
wfile . Using the reference to a variable will only write the variable name. The second argument to
wfile is the name of the file, entered without quotes. The file name can only use letters and numbers.
No other characters are allowed in the name of a file.
You can store a range or the resulting vector to a file, e.g.,
The contents of the corresponding files are shown below:
Page 2 © 2009 – Gilberto E. Urroz
988531807.012.png 988531807.013.png 988531807.014.png
Regarding the notation mat ( 2,4,6,10,12,14,16,18,9,1 ) written in file file04 : this specification contains
the 9 elements of the vector shown as s , above, plus the number of rows ( 9 ) and columns ( 1 ) defining
the vector. Consider, for example, the writing of matrices to files. In the following two examples, the
two matrices have 12 elements, each, but arranged differently. Matrix A has 3 rows and 4 columns,
while matrix B has 3 rows and four columns. They get stored into files file05 and file06 , respectively,
as shown below:
Next, we show the contents of the two files. Notice the specification of rows and columns as the last
two values in the list of data:
Here is another example of using function wfile :
The contents of the file are:
Page 3 © 2009 – Gilberto E. Urroz
988531807.001.png 988531807.002.png 988531807.003.png 988531807.004.png
Reading data from a file
To read data from a file use function rfile . The following examples use the files we created above (i.e.,
file01, file02, …, file07 ) to illustrate the use of function rfile . The result from reading data can be
simply shown by using an equal sign(=), or assigned to a variable by using the assignment operator
(:=). Try the following examples:
Additional examples of reading from a file are shown below:
If you attempt to read from a non-existing file you'll get an error message:
Page 4 © 2009 – Gilberto E. Urroz
988531807.005.png 988531807.006.png 988531807.007.png
Copying a matrix from a spreadsheet
Suppose you have the following 3x3 matrix (only the numbers, no labels, etc.) in an OpenOffice.org
Calc spreadsheet named myMatrix.ods:
Save the file in csv ( c omma- s eparated v alues) format into your user folder in your SMath Studio
installation:
Then, close the newly saved file myMatrix.csv, and open it using Notepad (right-click on the icon, and
select Open with … ):
Page 5 © 2009 – Gilberto E. Urroz
988531807.008.png 988531807.009.png 988531807.010.png
Zgłoś jeśli naruszono regulamin