smathstudio.0 85.iofiles.article.eng

8
 Input/Output of data and file manipulation in  SMath St udio 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 St udio 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 f rom and read data into SMath Studio, t"erefore, % copied t"e folder C!"ro#ram Files$%&6'!SMath! to a dif ferent location. %n my case t"e location c"osen #a s) C!Users!)ilberto + Urro!o.uments! UM0IC(12(""1IC(3IS!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!UM0IC(12(""1IC(3IS!SMat h!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 s ingle item of data into a file. -onsider t"e follo#ing eamples ) ou can o pen t"e files using, for eample,  ote5ad  in a Windows system to c"ec' t"e co ntents of t"e file) % recommend you use only lo#ercase l etters in t"e file names used to store data. % "ad difficultie s getting SMath Studio to read data out of files #it" uppercase letters in t"eir names. Page 1 © 2009 Gilberto E. Urroz

Upload: mikka

Post on 02-Jun-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: SMathStudio.0 85.IOFiles.article.eng

8/10/2019 SMathStudio.0 85.IOFiles.article.eng

http://slidepdf.com/reader/full/smathstudio0-85iofilesarticleeng 1/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, anddeleting an e isting file. !o illustrate t"e use of t"ese functions #e could start by #riting some data to

a file and e ploring t"e contents of t"e file. $efore #e do t"at, "o#ever, % #ant to address an issuerelated 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"edirectory C !"ro#ram Files$%&6'!SMath!SMath Studio! and it re(uires (dministrator access to modifyits contents. !o be able to #rite data from and read data into SMath Studio , t"erefore, % copied t"efolder 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(3I 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 oft"is ne# folder, in my case, is)

C !Users!)ilberto + Urro ! o.uments! UM 0IC(12(""1IC(3I 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 mydes'top. +o# #e proceed to illustrate t"e use of t"e wfile command.

Writing data to a fileou can use function wfile to #rite a single item of data into a file. -onsider t"e follo#ing e amples )

ou can open t"e files using, for e ample, ote5ad in a Windows system to c"ec' t"e contents of t"efile)

% recommend you use only lo#ercase letters in t"e filenames used to store data. % "ad difficulties getting SMath Studioto read data out of files #it" uppercase letters in t"eir names.

Page 1 © 2009 – Gilberto E. Urroz

Page 2: SMathStudio.0 85.IOFiles.article.eng

8/10/2019 SMathStudio.0 85.IOFiles.article.eng

http://slidepdf.com/reader/full/smathstudio0-85iofilesarticleeng 2/8

!"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 ofwfile. Using t"e reference to a variable #ill only #rite t"e variable name. !"e second argument towfile 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

Page 3: SMathStudio.0 85.IOFiles.article.eng

8/10/2019 SMathStudio.0 85.IOFiles.article.eng

http://slidepdf.com/reader/full/smathstudio0-85iofilesarticleeng 3/8

/egarding t"e notation mat ,4,6,78,7 ,74,76,7&,9,7 1 #ritten in file file84 ) t"is specification containst"e 9 elements of t"e vector s"o#n as s, above, plus t"e number of ro#s 91 and columns 71 definingt"e vector. -onsider, for e ample, t"e #riting of matrices to files. %n t"e follo#ing t#o e amples, t"et#o matrices "ave 2 elements, eac", but arranged differently. atri 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#)

+e t, #e s"o# t"e contents of t"e t#o files. +otice t"e specification of ro#s and columns as t"e lastt#o values in t"e list of data)

6ere is anot"er e ample of using function wfile)

!"e contents of t"e file are)

Page 3 © 2009 – Gilberto E. Urroz

Page 4: SMathStudio.0 85.IOFiles.article.eng

8/10/2019 SMathStudio.0 85.IOFiles.article.eng

http://slidepdf.com/reader/full/smathstudio0-85iofilesarticleeng 4/8

Reading data from a file!o read data from a file use function rfile . !"e follo#ing e amples use t"e files #e created above i.e.,

file87, file8 , ;, file8< 1 to illustrate t"e use of function rfile . !"e result from reading data can besimply s"o#n by using an e(ual sign 71, or assigned to a variable by using t"e assignment operator

)71. !ry t"e follo#ing e amples)

3dditional e amples of reading from a file are s"o#n belo#)

%f you attempt to read from a non8e isting file you*ll get an error message)

Page 4 © 2009 – Gilberto E. Urroz

Page 5: SMathStudio.0 85.IOFiles.article.eng

8/10/2019 SMathStudio.0 85.IOFiles.article.eng

http://slidepdf.com/reader/full/smathstudio0-85iofilesarticleeng 5/8

-opying a matri from a spreads"eetSuppose you "ave t"e follo#ing 4 4 matri only t"e numbers, no labels, etc.1 in an pen ffice.org-alc spreads"eet named m=Matri%+ods

Save t"e file in .s> . omma8 separated >alues1 format into your user folder in your SMath Studioinstallation)

!"en, close t"e ne#ly saved file m=Matri%+.s>,and open it using ote5ad rig"t8clic' on t"e icon, andselect 5en with ; 1)

Page 5 © 2009 – Gilberto E. Urroz

Page 6: SMathStudio.0 85.IOFiles.article.eng

8/10/2019 SMathStudio.0 85.IOFiles.article.eng

http://slidepdf.com/reader/full/smathstudio0-85iofilesarticleeng 6/8

to see t"e contents of t"e file)

-opy t"e file contents into anot"er te t file, let*s call it m=matri%file , and edit it to read)

!"us, t"e editing consisted in)

1. adding :mat : at t"e beginning of t"e line2. placing all t"e data into a single line3. replacing t"e missing commas bet#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)

f course, to get t"is simple 4 4 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 datasets.

2 3 fe# notes) 1 Use only lo#ercase letters in t"e name of t"e file. % couldn*t get SMath Studio to recognize filenamest"at include uppercase letters. 21 a'e sure t"e filename to be read "as no e tension, t"erefore, t"e file s"ould benamed, in t"is case, m=matri%file, and not m=matri%file+t%t or m=matri%file+dat , etc. 41 @o not try to simply use t"e same+.s> file #it"out an e tension as your input file to SMath Studio+ %t #on*t #or'. ou need to create a ne# file.

Page 6 © 2009 – Gilberto E. Urroz

Page 7: SMathStudio.0 85.IOFiles.article.eng

8/10/2019 SMathStudio.0 85.IOFiles.article.eng

http://slidepdf.com/reader/full/smathstudio0-85iofilesarticleeng 7/8

&riting and reading symbolic e pressions!"e follo#ing e amples illustrate t"e #riting of e pressions to a file, and t"e reading of t"e samee pressions from files.

!"e contents of t"e files are s"o#n belo#)

!"e follo#ing e ample s"o#s #riting and reading of a matri of symbolic e pressions)

Page 7 © 2009 – Gilberto E. Urroz

Page 8: SMathStudio.0 85.IOFiles.article.eng

8/10/2019 SMathStudio.0 85.IOFiles.article.eng

http://slidepdf.com/reader/full/smathstudio0-85iofilesarticleeng 8/8

!"e content of t"e file is s"o#n belo#)

@eleting filesUse function dfile filename 1 to delete files in t"e folder /*SMath Studio Installation Folder*/user .E amples)

!"e directory /*SMath Studio Installation Folder*/user is s"o#n belo# before and after t"e e ecutionof 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 storedata temporarily in a file.

Page 8 © 2009 – Gilberto E. Urroz