11.07.2015 Views

Advanced Programming Guide

Advanced Programming Guide

Advanced Programming Guide

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

156 • Chapter 4: Input and Output4.1 A Tutorial ExampleThis section illustrates how you can use the Maple I/O library in yourwork. Specifically, the examples show how to write a table of numericaldata to a file, and how to read such a table from a file. The examples referto the following data set, given in the form of a list of lists and assumedto represent a list of (x, y) pairs, where each x is an integer and each y isa real number.> A := [[0, 0],> [1, .8427007929],> [2, .9953222650],> [3, .9999779095],> [4, .9999999846],> [5, 1.000000000]]:In a real application, this list is generated by a Maple command or procedure.In this example, the list was simply entered as above.If you want to use another program (like a presentation graphics program,or perhaps a custom C program) to process data that Maple hasgenerated, then you often need to save the data to a file in a format thatthe other program recognizes. Using the I/O library, you can write suchdata to a file.> for xy in A do fprintf("myfile", "%d %e\n", xy[1], xy[2]) end do:> fclose("myfile");The file myfile is saved in the current directory. To determine the currentdirectory, use the currentdir() command. If you print the file myfile,or view it with a text editor, it looks like this:0 0.000000e-011 8.427008e-012 9.953223e-013 9.999779e-014 1.000000e+005 1.000000e+00The fprintf command writes each pair of numbers to the file. This commandtakes two or more arguments. The first argument specifies the filethat Maple is to write, and the second argument specifies the format forthe data items. The remaining arguments are the actual data items thatMaple is to write.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!