Matlab - Experimental data
Getting the data into matlab
- Using load:
For simple situations where all numbers have the same format, you can use
the load command. Suppose that you have a text file of data
(called foo, say) produced by another program, and that there
are 10 lines of 5 numbers. Then typing
load foo
will produce a matrix in matlab called "foo" with 10 rows and 5 columns. - Using textread:
If your text file format is less regular, read about the textread
routine. For example,
foo=textread('foo','','headerlines',7);
will ignore the first 7 lines of the file foo before reading numbers in the way that foo does. - Using tblread: If the data in already in a tabular format with labelled rows and columns, type "help tblread" to see how to get all the information inside matlab.
- Using low-level commands:
matlab has much the same file-reading capability as C. Both binary and
ASCII files can be read. Typing "help iofuns" lists the
commands.
You can resize the matrix of data inside matlab using the reshape command and also extract text from the file. Suppose you had the data file below (called foo, say)
The Title! 1 2 3 4 5 6 7 8 9 10 11 12
where you wanted the first line to be the title of your graph and the data to be stored in a 3 by 4 matrix. The following commands will set titlestr and data accordingly.fid = fopen('foo','r') titlestr=fgetl(fid) data=fscanf(fid,'%d') % For floats, use '%f' rather than '%d' reshape(data,4,3) fclose(fid)
Interpolation
Some of the more useful matlab commands require the data to be on a regular grid. Suppose you have experimental data z for a function of 2 variables, x and y, but those results don't lie on a regular grid (if you have no experimental date you can use the following to generate test data
v=1:100; x=100*rand(size(v)); y=100*rand(size(v)); z=50*rand(size(v));
). You can prepare this data for use with griddata
% First define a regular grid. Suppose x and y are between 0 and 100. % We'll set up a 201x201 grid steps = 0:.5:100; [XI,YI] = meshgrid(steps, steps); % XI and YI will both be 201x201. XI contains the x-coords of each point and % YI contains the y-coords. % now interpolate - find z values for these grid points ZI = griddata(x,y,z,XI, YI); % The z values in ZI can now be used by routines like contour, etc. % Display this as a mesh mesh(XI,YI,ZI); hold % plot the original data too plot3(x,y,z); hold off
Matlab 5.3 has improved 3D interpolation facilities. Type "help vissuite" to list the routines in the Visualization Suite.
In experimental data you might have missing values. The inpaint_nans routine should smoothly interpolate any nans that it finds in an array based on the neighbors of those points.