Join Curt Frye for an in-depth discussion in this video Importing and exporting data, part of Mathematica 10 Essential Training.
- View Offline
- So far in this movie, I have shown you how to perform various tasks using data that I typed in. Most data sets that you work with will be fairly large though. So it's unlikely you will type them in directly. In this movie, I will show you how to import and export data. I have created a new blank mathematical workbook and the first thing I need to do is bring my data in. I can use the import command to do that, but first I need to know one critical bit of information, and that is the full file path and name of the file that I want to import from.
In Windows Explorer, you can discover the full path to a file using a trick that I'll show you now. I'll press Alt-Tab to move to Windows Explorer, and I've already opened the Exercise Files directory. And you can see that I have a file called Create. I can discover the path to this particular folder by going up to the navigation bar, and clicking the folder icon to the left. So, I click that and I get C:\Users\Curt\Desktop\ExerciseFiles.
The text is highlighted so I can press Ctrl-C to copy it, and now I can press Alt-Tab to go back to Mathematica and start typing in my assignment and import command. I will assign the data I'm bringing in to a list called alist. So, I'll just type in a variable named alist, then use equal, and next I will type the import keyword, I-m-p-o-r-t, followed by a left square bracket, and then a double quotation mark.
Now because I copied my file path, I can press Ctrl-V, and when I do that, Mathematica displays a dialog box indicating that I am pasting text into a string and that my text contains either quotes or backslashes, and in my case, it does contain backslashes. It asks if I want to escape these characters so they appear verbatim in the string. And what that means is Mathematica uses backslashes and quotation marks in specific ways. If I want them to behave as characters as opposed to commands, then we do need to add escape characters.
So I will go ahead and click Yes. And you can see that I have two backslashes where I previously had one, and the first backslash indicates to Mathematica that what comes after it should be treated as text as opposed to a command. So, it's a little confusing, but basically this is how Mathematica allows you to use backslashes as text instead of commands. And now I can do one of two things. I can either click Create.csv, which is the name of the file that I want to link to, or that I want to import, or I can click File Browser to display Windows Explorer.
In this case, I do want to use Create.csv, so I'll click that, it's entered, I can type a double quotation mark, and a right square bracket, and I'll press Shift + Enter, and you can see that I have imported my data. If I want to display this data as a table as opposed to a list, I can type TableForm, followed by a left square bracket and alist, which is the name of my variable, right square bracket, and Shift + Enter, and there's the data displayed as a table.
The other thing you'll want to do fairly often is to export your data to a file. So, the same way that we imported data from a comma-separated values file, in this case we're exporting data to the same type of file. So what I need to do is to use the Export keyword. So, I'll type Export, then a left square bracket, and double quotation marks, I'm entering in the file path, and if I press Ctrl-V, so I'll click Yes, just like I did last time, and I'll give a new file name so I'll type a backslash, and I'll just call my file Export, E-x-p-o-r-t.csv, then a double quotation mark and a comma, because I haven't told Mathematica which variable to pull data from, and that variable is alist.
So, now it knows where to put the data, it knows which variable to bring the data in from, so I can type a right square bracket and press Shift + Enter, and my output appears to have worked. To verify that the export operation did work, I'll go back to Windows Explorer and there I see I have a new file called Export. In this movie, I showed you how to get a file path for a file or a folder in Windows. If you're working in MAC OS X, you can get the same data by control-clicking a file in Finder, and then clicking Get Info.
The path appears in the Get Info dialog box and you can copy it using your mouse and pressing Cmd-C.
- Managing notebooks
- Working with operators
- Assigning values to variables
- Importing and exporting data
- Creating advanced formulas
- Creating and manipulating lists
- Manipulating arrays
- Analyzing data with descriptive analytics
- Manipulating matrices
- Managing scripts
- Creating charts
- Formatting data