Ana səhifə

Introduction version 04/06/03


Yüklə 141.5 Kb.
səhifə2/3
tarix25.06.2016
ölçüsü141.5 Kb.
1   2   3

Getting data into Matlab programs

There are several ways to get data into Matlab programs. You may simply include the data in the program, or you may read the data from a binary or text file.



Include gridded data in program (cont4 is example)

"Gridded data" means:



  • The data were taken on a two dimensional grid in the field. The x and y spacings do not have to be equal.

  • There are no missing values.

  • The individual data entries do not have x y coordinates. That information is elsewhere, for example the top or bottom of the data table.

The opposite of gridded data is (are?) xyz data or randomly located data. A data set could be in xyz form but be gridded, but an xyz file can also contain randomly located data.


In cont4.m I have entered data in the program as a matrix representing the grid of data acquired in the field (without x and y values).

I have then created x and y values to use later in regridding the data.


d=[491 456 439 400 323 440 % data for the first y value, arrranged in order %of increasing x

415 438 283 358 356 345 % data for the first y value, arrranged in order %of increasing x


. several more lines of data go here

.

351 347 409 362 163.7 363



383 375 361 359 458 426];

y=5:2.5:50; % generate y values to go with data

x=0:5:25; % generate x values to go with data

Including x-z data in the program
There are reasons to include the x-location of the data, for example, you might acquire data at more closely spaced stations where the numbers were changing rapidly with distance.
You can enter the locations and readings into your program as a matrix, then parcel it out into x-value and data values. A program fragment to do this is shown below. The program with the entire data set is given in the program appendix as plottilt. Notice the use of the colon (:) to indicate all the rows in matrix d.
% vlf wave tilt data

% format is:

% location (tab) tilt in percent

d=[0 -26

10 -19

20 -1.2


. % lots more location and tilt data

.

390 -5



400 6];

x=d(:,1); % x is all the rows, column 1

tilt=d(:,2); % tilt is all the rows, column 2

subplot(2,1,1) % plot in upper half of screen

plot(x,tilt) % plot x values on horizontal axis, d on vertical axis

title('wave tilt')

xlabel('distance in meters')

ylabel('tilt in percent')


Please note, you could just as well use the same approach to enter x,y,z values.

Reading data in with Load


For larger data sets which are in text (ASCII) format in a separate file, it is more convenient to read the data into your Matlab program with the Matlab load command. . There is a little disadvantage to using this command. This command creates a variable with the same name as the file name, minus the extensionThe matrix name in your Matlab program must be the same as the file name (without the extension). If you have several data files to use in the same program you must rename them to the same name the program expects. There are two sneaky ways to overcoming this disadvantage. The first few lines of your matlab program can 1. read the original data file with the arbitrary name then 2. copy your data to a file with the name your program expects (outside of Matlab). You can execute system commands such as dir or copy from within matlab directly. In some cases you must preceed the command with a exclamation point. Trial and error are your best guide here.

You may have several data sets from one project that you would like to read into Matlab. You could give these data sets the same name with different extensions, for example they could be named p.1, p.2, p.3 etc. Then you use read which ever one you want into into your program into variable p.

If your data are in a text file with a specific format, say lines of header information followed by rows and columns of data, you should use readln. You may first have your program read the lines of header information and ignore them, then you may have your program read the rows and columns. The formatting commands are based on formats in C language, the Matlab documentation expects that you have some experience with a more basic programming such as C or Fortran is assumed if you are going to understand this. The best way to learn this is on a case by case basis. I will refrain from further comments at this time.



Reading from a binary file.
I have used Matlab to read SEG-Y seismic data files and Seismic Unix seismic or radar data files, and radar data files produced by Sensors and Software radar programs (.dt1 files). The Matlab command for reading these files is readf. It must be preceeded by an openf command and followed by a close command. The user must be know the specific binary format of the data such as 16 or 32 byte integer etc, and specify the format in the openf command. You can see an example of this in my program on displaying a cube of radar data, later.

Plotting data
My favorite feature in Matlab is plotting. It is easy to control basic plotting, label the axes, and annotate it. Plot is the basic plotting command.
If the argument is a single one-dimensional matrix then the points are plotted with the point count as the horizontal axis. For example:
x=rand(20,1); % generates 20 random numbers on the interval 0 to 1.

plot(x) % plots the values vs. point number



Plot with just one argument is useful for getting a quick look at a data set. One data set may be plotted against another by including both matrices in the argument.
For example:
t=0:0.1:2*pi % generates t values running from 0 to 2*pi in steps of 0.1

y=sin(t) % sin of the t values

plot(t,y) % plots the values of y versus t. (The t values are on the horizontal axis, the y %values are on the vertical axis)

The default line style is a continuous line. Other line styles may be indicated by a third argument within single quotes, or individual points may be plotted as printing characters. For example plot(a,b,'+ ') uses the values in in matrix a and b as the x and y coordinates and plots a + sign at those locations. You may also specify different colors to plot different data sets on the same axes.


Mini-homework: Modify the plot commands above but plot the values with + signs.

Mini-homework: modify the plot command to plot with other linestyles


Labelling plots
The axes of plots may be labeled with xlabel and ylabel commands, and a title labeled at the top of the graph with the title command. Note that these commands do not use equals signs and that the text inside the parentheses must be enclosed in single quotes.
For example add the following to the above program lines:
title('sine curve')

xlabel('time in secs')

ylabel('value of function')

1   2   3


Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©atelim.com 2016
rəhbərliyinə müraciət