Sei sulla pagina 1di 8

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 eisting file. !o illustrate t"e use of t"ese functions #e could start by #riting some data to
a file and eploring t"e contents of t"e file. $efore #e do t"at, "o#ever, % #ant to address an issue
related to t"e location of t"e SMath Studio installation folder.
The location of the SMath Studio installation folder
&"en % first attempted t"is after installing SMath Studio in my Windows Vista Ultimate 64-bit mac"ine
% 'ept getting a message indicating t"at access to t"e file location #as not permitted. !"is #as so
because SMath Studio, by default, sends output to and reads input from t"e folder /*SMath Studio
Installation Folder*/user/. %n my computer, t"e *SMath Studio Installation Folder* corresponds to t"e
directory C!"ro#ram Files$%&6'!SMath!SMath Studio! and it re(uires (dministrator access to modify
its contents. !o be able to #rite data from and read data into SMath Studio, t"erefore, % copied t"e
folder C!"ro#ram Files$%&6'!SMath! to a different location. %n my case t"e location c"osen #as)
C!Users!)ilberto *+ Urro,!-o.uments!/UM*0IC(12(""1IC(3I4/S!SMath
%n your computer it could be any location #"ere you don*t need (dministrator rig"ts to modify content.
% t"en proceeded to create t"e folder user, #"ere t"e data files #ould reside. !"us, t"e full address of
t"is ne# folder, in my case, is)
C!Users!)ilberto *+ Urro,!-o.uments!/UM*0IC(12(""1IC(3I4/S!SMath!SMath Studio!user
!o facilitate access to t"e program, at t"is point, % created a ne# s"ortcut to SMath Studio in my
des'top. +o# #e proceed to illustrate t"e use of t"e wfile command.
Writing data to a file
,ou can use function wfile to #rite a single item of data into a file. -onsider t"e follo#ing eamples
.
)
,ou can open t"e files using, for eample, /ote5ad in a Windows system to c"ec' t"e contents of t"e
file)
. % recommend you use only lo#ercase letters in t"e filenames used to store data. % "ad difficulties getting SMath Studio
to read data out of files #it" uppercase letters in t"eir names.
Page 1 2009 Gilberto E. Urroz
!"us, you*ll need to use t"e numerical result t"at you #ant to #rite out to a file as t"e first argument of
wfile. Using t"e reference to a variable #ill only #rite t"e variable name. !"e second argument to
wfile is t"e name of t"e file, entered #it"out (uotes. !"e file name can only use letters and numbers.
+o ot"er c"aracters are allo#ed in t"e name of a file.
,ou can store a range or t"e resulting vector to a file, e.g.,
!"e contents of t"e corresponding files are s"o#n belo#)
Page 2 2009 Gilberto E. Urroz
/egarding t"e notation mat06,4,6,78,76,74,76,7&,9,71 #ritten in file file84) t"is specification contains
t"e 9 elements of t"e vector s"o#n as s, above, plus t"e number of ro#s 091 and columns 071 defining
t"e vector. -onsider, for eample, t"e #riting of matrices to files. %n t"e follo#ing t#o eamples, t"e
t#o matrices "ave .2 elements, eac", but arranged differently. 2atri 3 "as 4 ro#s and 5 columns,
#"ile matri $ "as 4 ro#s and four columns. !"ey get stored into files file8: and file86, respectively,
as s"o#n belo#)
+et, #e s"o# t"e contents of t"e t#o files. +otice t"e specification of ro#s and columns as t"e last
t#o values in t"e list of data)

6ere is anot"er eample of using function wfile)
!"e contents of t"e file are)
Page 3 2009 Gilberto E. Urroz
Reading data from a file
!o read data from a file use function rfile. !"e follo#ing eamples use t"e files #e created above 0i.e.,
file87, file86, ;, file8<1 to illustrate t"e use of function rfile. !"e result from reading data can be
simply s"o#n by using an e(ual sign071, or assigned to a variable by using t"e assignment operator
0)71. !ry t"e follo#ing eamples)
3dditional eamples of reading from a file are s"o#n belo#)
%f you attempt to read from a non8eisting file you*ll get an error message)
Page 4 2009 Gilberto E. Urroz
-opying a matri from a spreads"eet
Suppose you "ave t"e follo#ing 44 matri 0only t"e numbers, no labels, etc.1 in an 9pen9ffice.org
-alc spreads"eet named m=Matri%+ods
Save t"e file in .s> 0.omma8separated >alues1 format into your user folder in your SMath Studio
installation)
!"en, close t"e ne#ly saved file m=Matri%+.s>, and open it using /ote5ad 0rig"t8clic' on t"e icon, and
select 45en with ;1)
Page 5 2009 Gilberto E. Urroz
to see t"e contents of t"e file)
-opy t"e file contents into anot"er tet file, let*s call it m=matri%file
6
, and edit it to read)
!"us, t"e editing consisted in)
1. adding :mat0: at t"e beginning of t"e line
2. placing all t"e data into a single line
3. replacing t"e missing commas 0bet#een ;.. and <.=, and bet#een 2.; and >.51
4. adding :4,41? at t"e end of t"e line.
Save t"is file and t"en try t"e command)
9f course, to get t"is simple 44 matri into SMath Studio it #ill be easier to type it in into t"e
#or's"eet. !"e procedure illustrated above #ould be more practical for very large spreads"eet data
sets.
2 3 fe# notes) 0.1 Use only lo#ercase letters in t"e name of t"e file. % couldn*t get SMath Studio to recognize filenames
t"at include uppercase letters. 021 2a'e sure t"e filename to be read "as no etension, t"erefore, t"e file s"ould be
named, in t"is case, m=matri%file, and not m=matri%file+t%t or m=matri%file+dat, etc. 041 @o not try to simply use t"e same
+.s> file #it"out an etension as your input file to SMath Studio+ %t #on*t #or'. ,ou need to create a ne# file.
Page 6 2009 Gilberto E. Urroz
&riting and reading symbolic epressions
!"e follo#ing eamples illustrate t"e #riting of epressions to a file, and t"e reading of t"e same
epressions from files.
!"e contents of t"e files are s"o#n belo#)
!"e follo#ing eample s"o#s #riting and reading of a matri of symbolic epressions)
Page 7 2009 Gilberto E. Urroz
!"e content of t"e file is s"o#n belo#)
@eleting files
Use function dfile0filename1 to delete files in t"e folder /*SMath Studio Installation Folder*/user.
Eamples)
!"e directory /*SMath Studio Installation Folder*/user is s"o#n belo# before and after t"e eecution
of t"e dfile commands s"o#n above)
3pplication of functions wfile, rfile, and dfile could be useful in programming if t"ere is a need to store
data temporarily in a file.
Page 8 2009 Gilberto E. Urroz

Potrebbero piacerti anche