11.07.2015 Views

Advanced Programming Guide

Advanced Programming Guide

Advanced Programming Guide

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

4.5 Input Commands • 175Example The following example shows a trivial use of readstat withina procedure.> InteractiveDiff := proc( )> local a, b;> a := readstat("Please enter an expression: ");> b := readstat("Differentiate with respect to: ");> printf("The derivative of %a with respect to %a is %a\n",> a,b,diff(a,b))> end proc:Reading Tabular DataThe readdata command reads TEXT files containing tables of data. Forsimple tables, this is more convenient than writing a procedure by usinga loop and the fscanf command.Use the following syntax to call the readdata command.readdata( fileIdentifier, dataType, numColumns )The fileIdentifier is the name or descriptor of the file from which readdatareads the data. The dataType must be one of integer or float, or youcan omit it, in which case readdata assumes float. If readdata needsto read more than one column, you can specify the type of each columnby using a list of data types.The numColumns argument indicates how many columns of data areto be read from the file. If you omit numColumns, readdata reads thenumber of columns specified by the number of data types that you specified(one column if you did not specify any dataType).If Maple reads only one column, readdata returns a list of the valuesread. If Maple reads more than one column, readdata returns a list oflists, each sublist of which contains the data read from one line of the file.If you call readdata with a filename, and that file is not open, Mapleopens it in READ mode as a TEXT file. Furthermore, if you call readdatawith a filename, it automatically closes the file when readdata returns.Example The following two examples are equivalent uses of readdatato read a table of (x, y, z)-triples of real numbers from a file.> A1 := readdata("my_xyz_file.text",3);A1 := [[1.5, 2.2, 3.4], [2.7, 3.4, 5.6], [1.8, 3.1, 6.7]]

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

Saved successfully!

Ooh no, something went wrong!