Docsity

Using MATLAB: A Primer and Data Input, Study notes of Experimental Physics

Toggle sidebar
bg1
Using MATLAB
A primer on MATLAB is given in Napolitano,
Appendix D, pp 419 ff
Running it
๏ƒ˜When you open it, it defaults to three
windows: Command, current
directory/workspace, history
๏ƒ˜Simple calculations can be made in the
command window step by step
๏ƒ˜Long or repeated calculations should be
done with a program (.m) file (start one by
clicking File..New..M File)
Inputting data:
๏ƒ˜x=[0:1:20] creates an array [0 1 2 3 ... 20]
(start at 0, increment in steps of one, until
you get to 20)
๏ƒ˜x=[1 3 5 3 8] creates an array with the
values given
๏ƒ˜load c:\mydata\mine.txt loads space or tab
delimited columns of ascii numbers into an
array of that name.
A matlab 6.5 variant is B=load('mine.dat'),
which names the vector array
(make sure you include the correct path to
the data)
๏ƒ˜A different approach is to read the data
from a file in a specific format
fid=fopen('sc1.lis');
a=fscanf(fid,'%f')';
fclose(fid);
pf3

Partial preview of the text

Download Using MATLAB: A Primer and Data Input and more Study notes Experimental Physics in PDF only on Docsity!

Using MATLAB

A primer on MATLAB is given in Napolitano, Appendix D, pp 419 ff Running it ๏ƒ˜ When you open it, it defaults to three windows: Command, current directory/workspace, history ๏ƒ˜ Simple calculations can be made in the command window step by step ๏ƒ˜ Long or repeated calculations should be done with a program (.m) file (start one by clicking File..New..M File) Inputting data: ๏ƒ˜ x=[0:1:20] creates an array [0 1 2 3 ... 20] (start at 0, increment in steps of one, until you get to 20) ๏ƒ˜ x=[1 3 5 3 8] creates an array with the values given ๏ƒ˜ load c:\mydata\mine.txt loads space or tab delimited columns of ascii numbers into an array of that name. A matlab 6.5 variant is B=load('mine.dat'), which names the vector array (make sure you include the correct path to the data) ๏ƒ˜ A different approach is to read the data from a file in a specific format fid=fopen('sc1.lis'); a=fscanf(fid,'%f')'; fclose(fid);

Useful points ๏ƒ˜ The colon operator uses the following rules to create regularly spaced vectors: j:kis the same as [j,j+1,...,k] ๏ƒ˜ Below are the definitions that govern the use of the colon to pick out selected rows, columns, and elements of vectors, matrices, and higher-dimensional arrays: A(:,j)is the j-th column of A B=A(:,1) creates a one-d array B that has the first column of A in it. A(i,:)is the i-th row of A A(i,j,k,:)is a vector in four-dimensional array A.