Numbers in, pictures out
Data and Files: Importing, Converting and Plotting Numerical Data

A calculation is only as good as the numbers that went in, and the results are only useful if they can be read. This section covers the unglamorous work around the mathematics: turning text into numbers, pulling tables from spreadsheets and databases, keeping scripts from failing silently, and drawing the result.
Getting numbers in
Text files are the common currency. Converting a string to a number looks trivial and is a classic source of error: a decimal comma instead of a point, a thousands separator, a stray unit or a blank field. The ASCII to double page covers the conversion and what to do when it fails. Spreadsheet files bring their own difficulties, such as merged headers and mixed text and number columns, discussed on the page about reading spreadsheet files into a matrix. Databases offer a third route through the SQL connection page.
Getting numbers out
Writing numbers has a mirror problem. Printing a double with too few digits loses information, while 17 significant digits are enough to reproduce any double exactly when it is read back. For files meant for humans, rounding is kinder. For files meant for another program, keep the digits.
Scripts that fail loudly
Batch work needs error handling. A script that continues after a failed read produces a plausible and wrong answer. Pages on trapping errors and on setting up a debugging session describe how to stop at the failure, inspect the state and decide whether to continue.
Plotting results
A table of numbers rarely convinces anyone; a picture often does. Surfaces of two variables are drawn as contour lines, and the choice of levels decides what the reader sees, a subject taken up on the contour levels page. Categories are compared with bar graphs, where a baseline at zero and equal bar widths keep the comparison honest. The wider topic, including how to match the picture to the question, has its own overview of data visualization.
Suggested order
Begin with conversion and import, since everything else depends on correct input. Add error handling next, and plotting last. After each step, run a round trip: write a matrix out, read it back and compare. A mismatch at that stage is far cheaper to fix than one found in the final graph.
Numerical methods that consume this data are in signal processing and statistics, and the glossary defines the vocabulary.
Questions about data files
Why do numbers read from a text file sometimes come out wrong?
Common causes are a different decimal separator, thousands separators, stray whitespace, hidden characters at the start of a file and inconsistent line endings. Check a few values after reading, compare the row and column counts with the source, and confirm the delimiter. A round trip, writing and reading back, exposes most problems.
What is the difference between a text and a binary file for numeric data?
Text files can be read and edited by people, and they survive moving between systems, but they are larger and may lose digits unless enough are written. Binary files store the exact bit pattern of each number, so they are compact and fast, but depend on byte order and number type.
How many digits should I write when saving floating-point numbers?
Seventeen significant digits are enough to reproduce any double-precision value exactly on reading. Fewer digits are fine for display or when the data are measured with limited precision. Saving too few digits silently changes results, which becomes visible only when a later computation amplifies the difference.
Why should a script stop at the first read error?
A failed read that is silently ignored leaves empty or partial data, and every later step then runs on the wrong input and may still produce plausible-looking output. Stopping early, with the file name and line in the message, points straight at the cause and keeps bad numbers out of results.
ASCII to Double: Converting Text to Floating-Point Numbers
Turning a string of ASCII characters into a double: the parsing steps, locale traps, and why the result is only the nearest binary value.
Bar Graphs: Uniform Bar Width, Spacing and Baselines
Drawing bar graphs with uniform width: how position and width relate, how to group bars, and why the baseline must start at zero.
Breakpoints in Scripts: Pausing Execution to Inspect State
Setting a breakpoint, stepping from it, and inspecting values: the core debugging loop for numerical scripts.
Comparing Two Files: Line Diffs and Numeric Tolerances
Ways to decide whether two files match and to list exactly what differs, including text diffs and tolerance-aware checks for numbers.
Contour Levels: Choosing Values for a Contour Plot
Picking the values at which contour lines are drawn: even spacing, round steps, explicit lists and logarithmic levels.
Contour Zones for Tecplot: Exporting Contour Lines
Computing contour lines from gridded data and writing each level as a zone for a plotting package that reads ASCII zone files.
Data Visualization for Numerical Results: Choosing the Plot
How to pick a plot for numerical results: start from the question, choose the encoding, then fix scales, color and labels.
Exporting Data to Tecplot: Writing ASCII Zone Files
Writing numerical results as a Tecplot ASCII data file: file structure, ordered zones, point order and checks before loading.
Graphing Results in SigmaPlot: Preparing Data Files
Preparing numerical output so a graphing package such as SigmaPlot can read it: layout, delimiters, missing values and verification.
Graphing sin x cos x: Period, Amplitude and Sampling
Plotting the product sin x cos x: simplify with a double-angle identity, find key points, then sample and draw.
Integer to String Conversion: Digits, Padding and Bases
Converting an integer to its text form: how the digits are produced, how to pad and sign them, and what happens when the input is not an integer.
Listing Files on an FTP Server: LIST, NLST and MLSD
Three ways an FTP server can list a directory, how to parse each reply, and how to select the files a script actually needs.
Mesh Plot with Contours: Building a meshc-Style Figure
A wireframe surface with contour lines projected underneath: how the grid is built and how to read the combined view.
Opening an FTP Connection: Anonymous and Password Login
What happens when a script opens an FTP connection, how anonymous and named logins differ, and which settings prevent hangs.
Plot Axis Intercepts: Where Axes Cross and Intercept Points
Intercepts of a function with the axes, and how a plot decides where its own axes cross.
Quoting Strings: Surrounding Quotes and Embedded Quotes
How to wrap a string in double quotes, remove surrounding quotes, and escape embedded quotes so delimited text survives a round trip.
Reading Spreadsheet Files into a Matrix: Ranges and Gaps
Getting numbers out of a spreadsheet and into a matrix: selecting a range, treating headers and blanks, and verifying the import.
Setting Up a Debugger Session for a Numerical Script
Preparing a debugging session so the failure repeats on demand: entry point, inputs, seeds, arguments and a record of findings.
SQL Connect: Opening a Database Connection from a Script
What a database connection needs, how to open and close one safely, and how to pull query results into a numeric matrix.
Surface Plot with Contours: Shaded Facets and Base Lines
A filled, color-mapped surface with contour lines beneath it: how facets are colored and when this view beats a wireframe.
Ternary Plots: Plotting Three-Component Compositions
Mapping three fractions that add up to one onto an equilateral triangle, with the conversion formula and a worked point.
Trapping Errors in Scripts: Try, Catch and Clean Up
Catching errors without hiding them: specific handlers, cleanup that always runs, bounded retries and useful messages.