Part 7: Data Manipulation
Go to Maple Portal Previous Tutorial Next Tutorial
Importing and Exporting Data
Statistics, Regression, and Curve Fitting
Maple's Tutorials are designed to help you get started with Maple, learn about the key tools available in Maple, and lead you through a series of problems.
In Part 7: Data Manipulation, learn to import and export data. Explore Maple's tools for statistics, visualization, and data analysis.
To try this material on your own, start with an empty Maple document. Perform the steps described in the left column of each table below. The results of the steps are displayed in the right column for reference.
Refer to Help>Quick Reference for basic getting started tips.
Note for non-Windows users: The keystrokes given in this document are for Windows. There will be differences for other platforms. If you are using a different platform, see Shortcut Keys.
Import and export data with interactive tools or with a command. With Maple, you can import data from many formats and export data back to files.
Using the Import Data Assistant
You can import data from a file. Supported file formats include Excel, MATLAB, Image, Audio, Matrix Market and Delimited.
Example: From the Tools>Assistants menu, choose Import Data...
Locate the data file ExcelData.xls. This file is located in the data/portal subdirectory of your Maple installation.
Choose Excel as the file type.
Click Next, Next, and Next. If desired, add a name so you can refer to this data later, and click Done.
The data is imported as an Array.
Note: After completing these steps to import the data, you can double-click on the summary to browse the data.
Now, use Plot Builder to plot the data.
From the Context Panel for the imported data, select Plots>Plot Builder.
30 x 2 MatrixData Type: anythingStorage: rectangularOrder: Fortran_order→
Using the ExcelTools Package
You can also use the ExcelTools package to import and export data stored in Microsoft Excel format.
Import the Excel file ExcelData.xls.
If a file is not located in the current directory, you need to enter the full path to the data file.
In this case, the data is located in the data/portal directory of your Maple installation. The command kernelopts(datadir) returns the location of the data directory. Then, the cat command is used to concatenate the two strings to give the full path to the data file.
View the first row.
Return the number of elements in the Matrix.
Plot the data using plots[pointplot].
DataSet≔ 30 x 2 MatrixData Type: anythingStorage: rectangularOrder: Fortran_order
Using the readdata Command
The readdata command reads numeric data from a text file. The calling sequences is readdata(fileID, n), where n is the number of columns of data.
The output from readdata is a list of elements.
This command returns a list, so use list selection to view entries in Data1. Then, plot the data.
The ImportMatrix command reads a data file just like readdata, but the output from ImportMatrix is a Matrix instead of a list.
Data2≔ 7876 x 2 MatrixData Type: anythingStorage: rectangularOrder: Fortran_order
Exporting an Excel File
Using ExcelTools, export to a file. By default, the file is exported to the current directory. To find what is the current directory, use the command currentdir().
As a test, import the first 10 rows back into Maple.
Exporting a Text File
The writedata command writes data to a text file. The calling sequence is writedata(fileID,data,format), where filename is the name given to the data file, data is the data itself, and format specifies how the data is to be written. The options for format are integer, float, or string.
Create a list, filling in the entries with the seq command.
Write the data to a file.
As a test, import the data file back into Maple.
To generate a random number, use the rand command. The simplest calling sequence generates a random 12 digit positive integer.
The rand(a..b) calling sequence returns a procedure which then generates numbers between a and b.
In order to ensure that a different procedure is created every time the code is run, the randomize command is used to reset the seed for the random number generator.
Generate random numbers between -10 and 10.
The rand command provides a simple interface to the RandomTools package, which consists of a large collection of tools and algorithms for random number and random object generation.
r ≔ rand−10..10:
Maple has a built-in Statistics package that provides a large number of continuous and discrete distributions that can be used to generate random numbers. To use these distributions, load the Statistics package.
Use a Normal distribution with a mean value of 5 and a standard deviation of 1 to generate 100 random numbers.
Create a line chart of the sample data.
From the Context Panel for the generated data, select Statistics>Visualization>Histogram.
After previewing the graph, click Quit to return the Histogram to your document.
1 .. 100 VectorrowData Type: float8Storage: rectangularOrder: Fortran_order
You can also define a random number generator using a distribution.
Here, X1 is a procedure that takes n as an argument, where n is the number of random numbers to generate.
Each time you use X1, it generates new random numbers.
Generate 10 random numbers using X1(10) and then create a line chart of the data using the LineChart command.
With Maple, it is easy to use statistics and to do curve fitting and regression analysis.
Many useful commands are available directly from the Context Panel.
Easy Access to Statistics Operations
For a 1-dimensional array, vector or list, the Context Panel includes Statistics operations. Under Statistics, the categories Data Manipulation, Quantities, Summary and Tabulation, and Visualization offer many Statistics commands.
Define a list as shown. In the Context Panel for the output, choose Statistics>Quantities>Mean.
The Curve Fitting Assistant is also available through the Context Panel.
In this example, we start by listing the pairs of data.
In the Context Panel for the data, select Curve Fitting>Interactive Curve Fitting. Select OK when prompted to specify the variable name. This opens the Curve Fitting Assistant which provides an easy way to access Maple's curve fitting commands.
You can choose the type of curve and preview the result. The function is displayed below the plot. Select the Splines curve, and select the corresponding Plot button.
You can opt to return the function (interpolant) or the plot. In the drop-down menu near the bottom left corner of the window, change the selection from Interpolant to Plot. Click Done.
The plot is displayed.
→curve fitting assistant
You can also call the Curve Fitting Assistant with a command. This allows you to pass data that was defined earlier in the Maple session.
Example: Use the DataSet that was imported in the Importing Data section of this tutorial. (If you have not done so already, go to this section first and follow the steps to import and define the data.)
Enter the command "CurveFitting[Interactive](DataSet)". The Curve Fitting Assistant opens.
Under Least Squares, click Plot to see the least squares curve.
At the bottom of the window, set the option to return the plot. In the drop-down menu near the bottom left corner of the window, change the selection from Interpolant to Plot. Click Done.
Advanced Example: Noisy Signal
In this example, you will use a random number generator to add noise to a signal.
Define the noise as a random number generator with mean value 1 and standard deviation 0.5.
We use a sample size of 200 to create a vector of noise values.
We can then extract entries from the vector. The vector indices go from 1 to 200. To select the ith entry, use the selection operator: PureNoiseVector[i].
Define a signal.
Create a noisy signal by adding noise to the original signal:
Generate a data set using the seq command.
Plot the signal and noisyData together.
To fit a functional model to the noisy data, use the Fit command from the Statistics package.
The calling sequence is Fit(f, X, Y, v), where f is the function model, X and Y are the x and y data, and v is the name of the independent variable in the component function.
Create two lists Xdata and Ydata from the data in the two columns of noisyData.
Tip: It's often useful to do something for each element of a data structure. Here, we do this by i=1..nops(noisyData). Recall, nops is the number of elements.
The function model is f=c⋅x⋅sina⋅x+b.
Plot f and the noisyData together to visualize the fit.
Tip: The Statistics and CurveFitting packages contain a number of commands for fitting curves to data points including linear, exponential, polynomial, least squares, and spline.
plotnoisyData,fx,x=−10..10,style=point,line,symbol=cross,legend=Noisy Signal, Fitted Model
Exercise: Repeat the noisy signal example using a random number generator based on a Weibull distribution.
Array, cat, currentdir, Curve Fitting (Maple Portal topic), CurveFitting package, kernelopts(datadir), list, map, rand, seq, Statistics
Download Help Document
What kind of issue would you like to report? (Optional)