Instructor: Bill Miller
Dynamic Neuroimaging Laboratory
415.502.3726
http://dnl.ucsf.edu
The help and reference information that comes with MatLab is essentially a list and description of functions and functionality. All their documentation unfortunately doesn't really teach you about how to use MatLab. If you want something more like a guide to how to use MatLab (more than my online notes), you might try one or more the books on this list. In addition, since MatLab includes its own versions of some useful commands from the C programming language, you may find a C-language reference book useful (also on the above linked page).
Examples
cd mydirectory no parens, string argument
plot(a) parens, non-string argument
plot(x,y) two arguments separated by a comma
1+2 adds 1+2 and displays the result (in a default variable called "ans")
a=1+2 creates the variable "a", and assigns it the value 3
b=a+10 creates "b", and assigns it the value 13 (assuming a=3)
a=[2 4 6] defines "a" as a vector with three values
a=[] "a" is variable with "null" (or empty set) as its value
a=rand(1,10); create a 1-row, 10-column array of random numbers (a built-in function)
b=a(3) b contains the third element of a
a = [1 2 3;4 5 6]; a is a two-row, three-column matrix. MatLab is silent when you type this.
a= [0:0.1:10] a contains the values 0, 0.1, 0.2, ... 10
b = [2 4 6;8 10 12]; if "b" is defined this way, then:
b(:,1) returns the values in all rows of first column of b
b(1,2:end) returns the values in columns 2-3 of first row, using "end"
--> note from the above example that arrays are indexed as "variable_name(rows,cols)"
a=[1:4], b=a' b is the transpose of a
c='bill was here' c is a string (type "whos" to see the type)
--> note in the first example above, a comma (or semicolon) can separate two or more commands on the same line.
a=rand(1,10); a is a row of random numbers
b=find(a<.5) b are indexes of values in "a" which are less than .5
a(b) obtain the values of those selected elements (indexes in b)
c=a(b) c is a new vector containing only those selected elements
Here are the basic logical operators: == (equals), ~= (not equals), <, >, <=, >=
Type "help ops" or "help ==" etc. for the whole list.
load flujet (A Matlab demo image; you don't need to use the ".mat" extension)
whos (look at all the variables just loaded from the one file)
load temp1.txt (creates the variable "temp1" from the file name
and puts the data from the file in there
- works with simple space delimited text files with no header)
--> here is the temp1.txt file to try. (Right click on the link and save it to a file with that name in a directory on your MatLab path).
save [filename] [variable(s)]
Example
a=1;b=2;c=3;
save myvars a b c saves all three variables in a mat file
called "myvars.mat" (extension not required)
clear clear all variables
load myvars a b load just "a" and "b" from myvars.mat
The most important file loading and saving functions are:
For brevity, we will just look at examples of reading files. Type "help iofun" for details about these and many more file input/output (IO) functions.
Examples
Text file
(temp1.txt has two columns
of floating point numbers, tab delimited)
fid=fopen('temp1.txt','rt'); open temp1.txt for "read text"
a=fscanf(fid,'%f %f',[2,101])' read all data into "a" (two rows by
101 columns, then transposed)
fclose(fid) close file
plot(a(:,1),a(:,2)) plot col 2 (y) against col 1 (x) - sine wave!
--> Note in the fscanf statement that we read in the values in the values assuming the data was 2 rows and 101 columns, and then transposed them, although the data was really saved as many rows and 2 columns. This is one of the relatively few non-intuitive instances of MatLab function design.
Binary file
(temp1.bin was created with
101x2 float values, same data as temp1.txt)
fid=fopen('temp1.bin','rb'); open temp1.bin for "read binary"
b=fread(fid,[101,2],'float'); read all data into "b"
fclose(fid); close file
plot(b(:,1),b(:,2)) plot the sine wave (x vs. y)
MatLab binary file
load temp1.mat temp1.mat contains one variable
"c" with the sine wave data (I've included the ".mat"
extension to allow MatLab to distinguish it
from "temp1.txt" in this particular example)
plot(c(:,1),c(:,2))
In summary, the above examples show how the same data can be loaded into MatLab although stored in files in different formats. Once loaded into a workspace variable in MatLab, the data is treated the same way (i.e., you can basically forget about how you loaded the data).
There are two other very useful functions for text file reading.
Finally, imread is a good basic function for loading pictures/images in many of the standard formats (jpeg, bmp, etc.).
In the next session, we will look at more about plotting data, creating figures with several plots on them, and working with figure properties; and introduce the basics of programming and function creation in MatLab.