Loading Data into MATLABLoading Data into MATLAB for PlottingIn addition to plotting values created with its own commands,MATLAB is very useful for plotting data from other sources,e.g., experimental measurements. Typically this data isavailable as a plain text file organized into columns. MATLABcan easily handle tab or space-delimited text, but it cannotdirectly import files stored in the native (binary) format ofother applications such as spreadsheets.The simplest way to import your data into MATLAB is with theload command. Unfortunately, the load commandrequires that your data file contain no text headings or columnlabels.
To get around this restriction you must use more advancedfile I/O commands. Below I demonstrate both approaches withexamples.
I've included an m-file to handle the more complex caseof a file with an arbitrary number of lines of text header, in additionto text labels for each column of data. Though hardly a cure-all, thisfunction is much more flexible than the load command becauseit allows you to provide documentation inside your data file.There is more than one way to read data into MATLAB from a file.The simplest, though least flexible, procedure is to use theload command to read the entire contents of the file ina single step. The load command requires that the datain the file be organized into a rectangular array. No columntitles are permitted. One useful form of the loadcommand isload name.extwhere ``name.ext' is the name of the file containing the data.The result of this operation is that the data in ``name.ext' isstored in the MATLAB matrix variable called name. The``ext' string is any three character extension, typically ``dat'.Any extension except ``mat' indicates to MATLAB that the datais stored as plain ASCII text.
A ``mat' extension is reservedfor MATLAB matrix files (see ``help load' for moreinformation).Suppose you had a simple ASCII file named myxy.dat that containedtwo columns of numbers. The following MATLAB statements will loadthis data into the matrix ``myxy', and then copy it into twovectors, x and y. load myxy.dat;% read data into the myxy matrix x = myxy(:,1);% copy first column of myxy into x y = myxy(:,2);% and second column into yYou don't need to copy the data into x and y, of course. Wheneverthe ``x' data is needed you could refer to it as myxy(:,1).Copying the data into ``x' and ``y' makes the code easier toread, and is more aesthetically appealing. The duplication ofthe data will not tax MATLAB's memory for most modest data sets.The load command is demonstrated in the.If the data you wish to load into MATLAB has heading information,e.g., text labels for the columns, you have the following optionsto deal with the heading text. Delete the heading information with a text editor and usethe load command:-(.
Use the fgetl command to read the headinginformation one line at at time. You can then parsethe column labels with the strtok command.This technique requires MATLAB version 4.2c or later.
Use the fscanf command to read the headinginformation.Of these options, using fgetl and strtok isprobably the most robust and convenient. If you read the headingtext into MATLAB, i.e., if you don't use the loadcommand, then you will have to also read the plot data withfscanf. The example,shows how this is done.This example show you how to load a simple data set and plot it.Thefile contains two columns of numbers. The first is the number ofthe month, and the second is the mean precipitationrecorded at the Portland International Airport between 1961 and 1990.(For an abundance of weather data like this check out the)Here are the MATLAB commands to create a symbol plotwith the data from PDXprecip.dat. These commands are alsoin the script filefor you to download.
load PDXprecip.dat;% read data into PDXprecip matrix month = PDXprecip(:,1);% copy first column of PDXprecip into month precip = PDXprecip(:,2);% and second column into precip plot(month,precip,'o');% plot precip vs. Month with circles xlabel('month of the year');% add axis labels and plot title ylabel('mean precipitation (inches)'); title('Mean monthly precipitation at Portland International Airport');Although the data in the month vector is trivial, itis used here anyway for the purpose of exposition.
The precedingstatments create the following plot.If all your data is stored in files that contain no text labels,the load command is all you need. I like labels,however, because they allow me to document and forget about thecontents of a file. To use the load for such a fileI would have to delete the carefully written comments everytimeI wanted to make a plot. Then, in order to minimize my effort,I might stop adding the comments to the data file in the first place.For us control freaks, that leads to an unacceptable increasein entropy of the universe! The solution is to find a wayto have MATLAB read and deal with the text comments at the topof the file.The following example presents a MATLAB function thatcan read columns of data from a file when the file has anarbitrary length text header and text headings for each columns.The data in the fileis reproduced belowMonthly averaged temperature (1961-1990) Portland International AirportSource: Dr. George Taylor,Temperatures are in degrees FarenheitMonth High Low Average145.3633.8439.6250.8735.9843.5547.3460.4941.3650.9257.863.4368.7968.8363.1.9554.5.5446.7540.17The file has a five line header (including blank lines) andeach column of numbers has a text label.
To use this data withthe load command you would have to delete the text labelsand save the file. A better solution is to have MATLAB read thefile without destroying the labels.
Better yet, we should be ableto tell MATLAB to read and use the column headings when it createsthe plot legend.There is no built-in MATLAB command to read this data, so wehave to write an m-file to do the job.