Thursday, December 23, 2010

importing data into MatLab

My biggest loathing in scripting is importing data. I hate it. So today I'm going to tell you how to do it in Matlab, nice and easy.
1. go to the file menu, and you will see a nice button that says Set Path (you'll see it, click file)

2. You will see an add folder option. Choose a folder that you want to keep stuff in. There's no penalty to too many folders, so I recommend making a new one for every new big project that you do (i.e. "biomass", "protein interactions," etc.)
As you can see my computer user name is Sparky, after my middle name.

3. Now you will be ready to go! Make sure you've made some files that you want to put information in. Now open up your program. Let's say you have a file named real_heigh_hist.csv you want to import (it's a nice little matrix or something, with no titles, of course, because Matlab hates titles)

4. In the MatLab prompt in your script, say:

FILE = importdata('real_heigh_hist.csv',',');

That means-- pull in this file called real_height_hist.csv, and that he items are separated by commas (csv-- comma separated values)
after you've done that for as many FILES as you want (hint: label them FILE, FILE2, FILE3, FILE4), then you will want to rename them something meaningful, like "height_hist"
Now, save your file as something nice and open it in Matlab's editor. Tada! You can call your variable (height_hist) or you can add more information to your script and have fun. 

*note: some people may find this oversimplistic-- but I know the biggest barrier for me learning to script was importing files.

No comments:

Post a Comment