Join Curt Frye for an in-depth discussion in this video Importing and exporting data in Mathematica, part of Learning Mathematica 9.
Throughout this course, i have shown you how to, perform your calculations on data that we typed in by lists. Most data sets that you work with will be fairly large so it's unlikely that you'll be typing in data directly. So in this movie, I will show you how to import and export data. I have created a new blank, Mathematica notebook. And the first thing I need to do, is to bring in my files. To do, that I can use the Import command but I need to know one critical bit of information, the full file path and name of the file that I want to import from.
And in Windows, there is actually a fairly straight forward way to get that, even within the graphical user interface. So i'll switch over, to my extra size files folder, inside of File Explorer. And here you can see that I have a coma separated values file called create. It's important that you save your data, in a coma separated value, or C-S-V file. It's a very common format, its text based it allows you to save as much data as you want, with no real restrictions. So, it's just the interchange format of choice and I highly recommend that you use it.
So what you want to do, is click the create file to get the same file path information on the Mac, control click the file and finder, and click get info. The file path appears next to the word where. You can copy the text and paste it into Mathematica. And then you need to find out, the path that points to this file. To do that in Windows, in the File Explorer, go up to the Navigation bar, here at the top. And click this folder, it's to the left of this right pointing arrow.
Click that, and you can see that the value inside of the navigation bar changes to the file path. So, press Ctrl, C to copy it and then switch back over to Mathematica. Now we can type in our import command. The first thing we need to do, is assign the data to a variable. So I'll call it, I list, just for import list. Then equal. Then import. A left square bracket, and then a double quote. Now I copied the file path earlier by pressing Ctrl C.
So now I'll paste it by pressing Ctrl V. I get a, morning dialogue indicating that I am pasting, data into Mathematica. It wants to make sure I intend to do that. So I click yes. And I get, my string. It's not complete, but it's a great start. What you can see is that, I have the name of my drive, which is C, then a //. In Mathematica, you have control characters. So that means that there are certain characters that are reserved. But if you use a / you can indicate that the next character should be interpreted literally.
So what's happening here, is that C:\ users is being rendered as C:\\ users. So don't let that throw you off. It is intentional. Then I have Kurt desktop, extra size files. And then I'll type in two back-slashes, and then the name of the file, which is create.csv. Then a double quotation mark and a right square bracket. And, I'll press Shift-Enter, to evaluate the expression.
So you can see, that the variable I list, now contains, a list of data and it starts out with the first row of balance, one, 51,992 and so on to the end. If I want to display this data as a table. I can use the command TableForm, so that's capital T-a-b-l-e, form, Capital F-o-r-m, then a left square bracket, and then the name of the variable, I list. Right square bracket, and Shift, Enter, and there's my table in the standard tabular form.
And now that I have the data in the I list variable. I could work with it, using the techniques that I've demonstrated elsewhere. So that's how you bring data in. Now let me show you how to send data out, by exporting. To export data, you use the export keyword. Probably not a surprise. Then a left square bracket. And then a double quote. Now in the same way that you specified the directory to import data from, you need to specify the direcory that you're exporting data to. If you're exporting data to the same folder that contains the mathematical program, you don't need to do anything, but in this case I'll send it back to my exercise files folder.
And because I still have, that path stored inside the clipboard. I'll press Ctrl V. Indicate that yes, I do want to paste that control value. There it is. I'll type a double back slash. And then, I'll call it, exported. That's cvs, double quotes. Now, I'll type a comma. And I need to identify the variable that contains the data I want to export, and that is I List. Now I type my right square bracket, everything looks good, and I press Shift Enter.
And I get a confirmation that the file has been created, and I can see it, if I shift over to my exercise files folder. And there it is! I can open it up and work with it to my heart's content.
Curt Frye teaches you how to set up Mathematica notebooks, assign values to variables, perform simple calculations, create and manipulate matrices, enter equations in linear and descriptive form, write and debug Mathematica scripts, and visualize data with charts.
NOTE: Basic knowledge of linear algebra is helpful for this course, but not required.
- Managing Mathematica workbooks
- Assigning values to variables
- Calculating values with built-in functions
- Manipulating matrices
- Importing and exporting data
- Defining functions
- Creating charts