 7-Data Manipulation - Maple Help

Home : Support : Online Help : Getting Started : Tutorials : 7-Data Manipulation Part 7: Data Manipulation Introduction 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. Importing and Exporting Data

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. Importing Data

Steps

Result

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.

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 a Matrix.

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.

Click Plot. $\to$ Using the ExcelTools Package

You can also use the ExcelTools package to import and export data stored in Microsoft Excel format.

Example:

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].



$\mathrm{with}\left(\mathrm{ExcelTools}\right):$ $\left[\begin{array}{cc}0.0& 0.918632608623005\end{array}\right]$ (2.1.1)

 ${60}$ (2.1.2) 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.

$\mathrm{FileLocation2}≔\mathrm{cat}\left(\mathrm{kernelopts}\left(\mathrm{datadir}\right),"/portal/DataP2.dat"\right):$

$\mathrm{Data1}≔\mathrm{readdata}\left(\mathrm{FileLocation2},2\right):$

$\mathrm{nops}\left(\mathrm{Data1}\right)$

 ${3938}$ (2.1.3)

$\mathrm{Data1}\left[1\right]$

 $\left[{0.008}{,}{-2.828784}\right]$ (2.1.4)

$\mathrm{Data1}\left[100\right]$

 $\left[{0.007901}{,}{-0.942928}\right]$ (2.1.5)

$\mathrm{plot}\left(\mathrm{Data1},\mathrm{style}=\mathrm{point},\mathrm{symbol}=\mathrm{cross},\mathrm{color}="Brown"\right)$ The ImportMatrix command reads a data file just like readdata, but the output from ImportMatrix is a Matrix instead of a list.

$\mathrm{Data2}≔\mathrm{ImportMatrix}\left(\mathrm{FileLocation2}\right)$ $\mathrm{plot}\left(\mathrm{Data2},\mathrm{style}=\mathrm{point},\mathrm{color}="Green"\right)$  Exporting Data

Steps

Result

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.

$\mathrm{with}\left(\mathrm{ExcelTools}\right):$

$\mathrm{Export}\left(\mathrm{Data3},"TestFile.xls"\right)$

$\mathrm{Import}\left("TestFile.xls","Sheet1","A1:B10"\right)$

 $\left[\begin{array}{cc}{0.008}& {-2.828784}\\ {""}& {""}\\ {0.007999}& {-2.828784}\\ {""}& {""}\\ {0.007998}& {-2.69408}\\ {""}& {""}\\ {0.007997}& {-2.69408}\\ {""}& {""}\\ {0.007996}& {-2.559376}\\ {""}& {""}\end{array}\right]$ (2.2.1)



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.

Example:

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.

$\mathrm{TestData}≔\left[\mathrm{seq}\left(\left[0.1\cdot i,{\left(0.1\cdot i\right)}^{2}\right],i=0..100\right)\right]:$

$\mathrm{TestData}\left[3\right]$

 $\left[{0.2}{,}{0.04}\right]$ (2.2.2)



$\mathrm{writedata}\left("MyDataFile",\mathrm{TestData},\mathrm{float}\right)$



$\mathrm{readdata}\left("MyDataFile",2\right)$

 $\left[\left[{0.}{,}{0.}\right]{,}\left[{0.1}{,}{0.01}\right]{,}\left[{0.2}{,}{0.04}\right]{,}\left[{0.3}{,}{0.09}\right]{,}\left[{0.4}{,}{0.16}\right]{,}\left[{0.5}{,}{0.25}\right]{,}\left[{0.6}{,}{0.36}\right]{,}\left[{0.7}{,}{0.49}\right]{,}\left[{0.8}{,}{0.64}\right]{,}\left[{0.9}{,}{0.81}\right]{,}\left[{1.}{,}{1.}\right]{,}\left[{1.1}{,}{1.21}\right]{,}\left[{1.2}{,}{1.44}\right]{,}\left[{1.3}{,}{1.69}\right]{,}\left[{1.4}{,}{1.96}\right]{,}\left[{1.5}{,}{2.25}\right]{,}\left[{1.6}{,}{2.56}\right]{,}\left[{1.7}{,}{2.89}\right]{,}\left[{1.8}{,}{3.24}\right]{,}\left[{1.9}{,}{3.61}\right]{,}\left[{2.}{,}{4.}\right]{,}\left[{2.1}{,}{4.41}\right]{,}\left[{2.2}{,}{4.84}\right]{,}\left[{2.3}{,}{5.29}\right]{,}\left[{2.4}{,}{5.76}\right]{,}\left[{2.5}{,}{6.25}\right]{,}\left[{2.6}{,}{6.76}\right]{,}\left[{2.7}{,}{7.29}\right]{,}\left[{2.8}{,}{7.84}\right]{,}\left[{2.9}{,}{8.41}\right]{,}\left[{3.}{,}{9.}\right]{,}\left[{3.1}{,}{9.61}\right]{,}\left[{3.2}{,}{10.24}\right]{,}\left[{3.3}{,}{10.89}\right]{,}\left[{3.4}{,}{11.56}\right]{,}\left[{3.5}{,}{12.25}\right]{,}\left[{3.6}{,}{12.96}\right]{,}\left[{3.7}{,}{13.69}\right]{,}\left[{3.8}{,}{14.44}\right]{,}\left[{3.9}{,}{15.21}\right]{,}\left[{4.}{,}{16.}\right]{,}\left[{4.1}{,}{16.81}\right]{,}\left[{4.2}{,}{17.64}\right]{,}\left[{4.3}{,}{18.49}\right]{,}\left[{4.4}{,}{19.36}\right]{,}\left[{4.5}{,}{20.25}\right]{,}\left[{4.6}{,}{21.16}\right]{,}\left[{4.7}{,}{22.09}\right]{,}\left[{4.8}{,}{23.04}\right]{,}\left[{4.9}{,}{24.01}\right]{,}\left[{5.}{,}{25.}\right]{,}\left[{5.1}{,}{26.01}\right]{,}\left[{5.2}{,}{27.04}\right]{,}\left[{5.3}{,}{28.09}\right]{,}\left[{5.4}{,}{29.16}\right]{,}\left[{5.5}{,}{30.25}\right]{,}\left[{5.6}{,}{31.36}\right]{,}\left[{5.7}{,}{32.49}\right]{,}\left[{5.8}{,}{33.64}\right]{,}\left[{5.9}{,}{34.81}\right]{,}\left[{6.}{,}{36.}\right]{,}\left[{6.1}{,}{37.21}\right]{,}\left[{6.2}{,}{38.44}\right]{,}\left[{6.3}{,}{39.69}\right]{,}\left[{6.4}{,}{40.96}\right]{,}\left[{6.5}{,}{42.25}\right]{,}\left[{6.6}{,}{43.56}\right]{,}\left[{6.7}{,}{44.89}\right]{,}\left[{6.8}{,}{46.24}\right]{,}\left[{6.9}{,}{47.61}\right]{,}\left[{7.}{,}{49.}\right]{,}\left[{7.1}{,}{50.41}\right]{,}\left[{7.2}{,}{51.84}\right]{,}\left[{7.3}{,}{53.29}\right]{,}\left[{7.4}{,}{54.76}\right]{,}\left[{7.5}{,}{56.25}\right]{,}\left[{7.6}{,}{57.76}\right]{,}\left[{7.7}{,}{59.29}\right]{,}\left[{7.8}{,}{60.84}\right]{,}\left[{7.9}{,}{62.41}\right]{,}\left[{8.}{,}{64.}\right]{,}\left[{8.1}{,}{65.61}\right]{,}\left[{8.2}{,}{67.24}\right]{,}\left[{8.3}{,}{68.89}\right]{,}\left[{8.4}{,}{70.56}\right]{,}\left[{8.5}{,}{72.25}\right]{,}\left[{8.6}{,}{73.96}\right]{,}\left[{8.7}{,}{75.69}\right]{,}\left[{8.8}{,}{77.44}\right]{,}\left[{8.9}{,}{79.21}\right]{,}\left[{9.}{,}{81.}\right]{,}\left[{9.1}{,}{82.81}\right]{,}\left[{9.2}{,}{84.64}\right]{,}\left[{9.3}{,}{86.49}\right]{,}\left[{9.4}{,}{88.36}\right]{,}\left[{9.5}{,}{90.25}\right]{,}\left[{9.6}{,}{92.16}\right]{,}\left[{9.7}{,}{94.09}\right]{,}\left[{9.8}{,}{96.04}\right]{,}\left[{9.9}{,}{98.01}\right]{,}\left[{10.}{,}{100.}\right]\right]$ (2.2.3) Random Distributions

Steps

Result

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.

Example:

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.

$\mathrm{rand}\left(\right)$

 ${395718860534}$ (3.1)

























$\mathrm{randomize}\left(\right):$

$r\left(\right);$

 ${-}{5}$ (3.2)

$r\left(\right);$

 ${5}$ (3.3)

$r\left(\right);$

 ${-}{4}$ (3.4)



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.

Example:

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.







$\mathrm{with}\left(\mathrm{Statistics}\right):$



$X≔\mathrm{RandomVariable}\left(\mathrm{Normal}\left(5,1\right)\right):$

$\mathrm{Sample}\left(X,100\right)$

 $\left[\begin{array}{cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc}3.9275758720017313& 4.670922129452935& 4.382908063090211& 5.214466745245291& 4.9745718719576155& 6.728821284177832& 3.3651432456560952& 6.5711721717543& 5.170358421410408& 6.006478408751809& 5.268712679158311& 3.534019553041223& 4.964242146160504& 4.993496636537364& 4.942816997766762& 4.815399905375071& 5.203855402371112& 4.901128175972876& 4.917301902958018& 4.831880871878592& 5.1620212998679325& 4.324081290327383& 4.559202355951467& 5.89601998525955& 5.461022953380024& 6.277977052273846& 5.234565961794581& 4.963725508706317& 4.551151876487854& 3.832626914675288& 3.7820571160587555& 3.8453080390737506& 5.109659323283598& 4.380190619358718& 5.642151004644923& 4.829229081901756& 4.179947758972018& 6.34633519663234& 4.013961439841875& 4.034161206500152& 3.635374280194256& 6.650078719564077& 6.348041513520173& 3.9746260738134094& 5.22978806086391& 6.484934579680526& 3.699690945884128& 4.623868883763793& 5.061784196667206& 5.801907829141039& 4.870024529449141& 5.112484530604132& 5.630360591794658& 4.979104060315827& 4.169557177002601& 4.7373474898588155& 4.299699738031992& 6.955569058612731& 4.740525834377393& 4.91791493743757& 6.215579452624995& 4.016868186240343& 6.930690648889354& 5.1227351085867525& 5.4329454211515396& 4.475556182137195& 5.2242155519278315& 4.585774431901628& 5.035526136229639& 6.983712097679989& 7.213379589390357& 5.881229888626876& 5.886856450560421& 4.138334052691198& 4.093388696659107& 4.123269015650282& 4.782509540380011& 4.663844825710863& 5.549376576019131& 3.802451565951668& 4.072489660460477& 3.670049394577724& 3.8695639685302217& 6.271870250670021& 3.523484536550944& 6.613382490936093& 4.314657930632515& 6.082412900920643& 4.223265099520133& 4.444518561905356& 6.072391847768252& 5.071352977629555& 4.817877075510444& 5.628549543618576& 4.274149354989713& 4.586619661399977& 6.5424153011566055& 4.7796905725761905& 4.159183975247284& 5.3850660120666465\end{array}\right]$ (3.5)

$\stackrel{\text{histogram}}{\to }$ 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.

Example:

Generate 10 random numbers using X1(10) and then create a line chart of the data using the LineChart command.







$\mathrm{X1}≔\mathrm{Sample}\left(\mathrm{RandomVariable}\left(\mathrm{Normal}\left(5,1\right)\right)\right)$

 ${\mathrm{X1}}{≔}{\mathbf{proc}}\left({n}{::}{\mathrm{sizeType}}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{...}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (3.6)

$\mathrm{X1}\left(1\right)$

 $\left[\begin{array}{c}{5.56775664743282}\end{array}\right]$ (3.7)

$\mathrm{X1}\left(1\right)$

 $\left[\begin{array}{c}{4.94414995600839}\end{array}\right]$ (3.8)

$\mathrm{X1}\left(3\right)$

 $\left[\begin{array}{ccc}{5.61495983207839}& {5.29958264902437}& {5.30232746471033}\end{array}\right]$ (3.9)





$\mathrm{Data}≔\mathrm{X1}\left(10\right)$

 ${\mathrm{Data}}{≔}\left[\begin{array}{cccccccccc}{4.63283345109185}& {5.41578550493451}& {3.65768254593162}& {3.23660602451747}& {3.92222331254421}& {5.41587916509957}& {4.57745294314229}& {4.91588247900506}& {3.89064379925430}& {3.94021634676111}\end{array}\right]$ (3.10)

$\mathrm{LineChart}\left(\mathrm{Data},\mathrm{color}=\mathrm{red},\mathrm{thickness}=3,\mathrm{symbol}=\mathrm{circle},\mathrm{symbolsize}=10,\mathrm{legend}=\left["Data"\right]\right)$   Statistics, Regression, and Curve Fitting Basic Tools

Steps

Results

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.

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.

Example:

Define a list as shown.  In the Context Panel for the output, choose Statistics>Quantities>Mean.























 $\left[{0.8329091229}{,}{1.526056303}{,}{1.931521412}{,}{2.219203484}{,}{2.442347035}{,}{2.624668592}{,}{2.778819272}{,}{2.912350665}{,}{3.030133700}{,}{3.135494216}{,}{3.230804396}{,}{3.317815773}\right]$ (4.1.1)

$\stackrel{\text{mean}}{\to }$

 ${2.49851033090833}$ (4.1.2)







The Curve Fitting Assistant is also available through the Context Panel.

Example:

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.











 $\left[\left[{0}{,}{3}\right]{,}\left[{1}{,}{1}\right]{,}\left[{2}{,}{7}\right]{,}\left[{3}{,}{6}\right]{,}\left[{4}{,}{9}\right]{,}\left[{5}{,}{11}\right]\right]$ (4.1.3)

$\stackrel{\text{curve fitting assistant}}{\to }$ 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.)

 1 Enter the command "CurveFitting[Interactive](DataSet)".  The Curve Fitting Assistant opens.

 2 Under Least Squares, click Plot to see the least squares curve.

 3 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.

The plot is displayed.







$\mathrm{CurveFitting}\left[\mathrm{Interactive}\right]\left(\mathrm{DataSet}\right);$  Steps

Results

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 $i$th entry, use the selection operator: PureNoiseVector[i].





$\mathrm{with}\left(\mathrm{Statistics}\right):$

$\mathrm{Noise}≔\mathrm{Sample}\left(\mathrm{RandomVariable}\left(\mathrm{Normal}\left(1,0.5\right)\right)\right):$







$\mathrm{PureNoiseVector}≔\mathrm{Noise}\left(200\right):$

$\mathrm{PureNoiseVector}\left[1\right]$

 ${1.14175247336227}$ (4.2.1)



Define a signal.

Create a noisy signal by adding noise to the original signal:

$\mathrm{signal}\left(0.1\left(i-100\right)\right)+{\mathrm{PureNoiseVector}}_{}$[i]

Generate a data set using the seq command.

Plot the signal and noisyData together.





$\mathrm{signal}:=x→x\mathrm{sin}\left(x\right):$



$\mathrm{noisyData}≔\left[\mathrm{seq}\left(\left[0.1\cdot \left(i-100\right),\mathrm{signal}\left(0.1\cdot \left(i-100\right)\right)+\mathrm{PureNoiseVector}\left[i\right]\right],i=1..200\right)\right]:$

$\mathrm{plot}\left(\left[\mathrm{noisyData},\mathrm{signal}\left(x\right)\right],\mathrm{style}=\mathrm{point},\mathrm{symbol}=\left[\mathrm{cross},\mathrm{circle}\right],\mathrm{color}=\left["Green","Blue"\right],\mathrm{legend}=\left["Noisy Signal","Signal"\right]\right)$ Curve Fitting

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\cdot x\cdot \mathrm{sin}\left(a\cdot x\right)+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.



















$\mathrm{Xdata}≔\left[\mathrm{seq}\left(\mathrm{noisyData}\left[i,1\right],i=1..\mathrm{nops}\left(\mathrm{noisyData}\right)\right)\right]:$

$\mathrm{Ydata}≔\left[\mathrm{seq}\left(\mathrm{noisyData}\left[i,2\right],i=1..\mathrm{nops}\left(\mathrm{noisyData}\right)\right)\right]:$







$f≔\mathrm{Fit}\left(c\cdot x\cdot \mathrm{sin}\left(a\cdot x\right)+b,\mathrm{Xdata},\mathrm{Ydata},x\right)$

 ${f}{≔}{1.01118654034700}{x}{\mathrm{sin}}{}\left({1.00093056049853}{x}\right){+}{1.08883811779035}$ (4.2.2) Exercise:  Repeat the noisy signal example using a random number generator based on a Weibull distribution. See Also