1. Home

Numbers in, pictures out

Data and Files: Importing, Converting and Plotting Numerical Data

Plaster relief tile of a rolling surface with engraved contour lines (AI-generated image)
AI-generated image

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.

Back to top