Title: Introduction To Matlab Class 4
1Introduction To MatlabClass 4
- Instructors Hristiyan (Chris) Kourtev
- and Xiaotao Su, PhD
- Double click the matlab icon
- When prompted click Skip
2Managing data
- To manage basic numerical data we use matrices
- our_data 1, 4, 10.59, 12
2, 9, 18.76, 5 3, 7,
1.13, 2
Trial 1
Trial 2
Trial 3
Trial Number
Response2 (perhaps the response time)
Response3
Response1
3- our_data 1, 4, 10.59, 12
2, 9, 18.76, 5 3, 7,
1.13, 2 - To set the 4th row (4th trial) data
- our_data(4, ) 4, 7, 2.93, 3
- or we could
- our_data(5, 1) 5
- our_data(5, 2) 1
- our_data(5, 3) 2.10
- our_data(5, 4) 9
- To get this 4th row data
- trial_data our_data(4, )
gets or sets all elementsfrom row or column
4 disp('I am thinking of a number between')
disp(' 1 and ', num2str(highest_number))
response input('What is it?') stop_time
GetSecs response_time stop_time -
start_time is_correct (responserandom_nu
m) if(is_correct)
disp('Right!') else disp('Wrong!
The correct answer was ', ...
num2str(random_num)) end disp('Press
any key to continue') pause
disp('--------') record data
guessing_game_data(t, ) ... t,
response_time, response, ... random_num,
is_correct end
- guessing_game.m
- test to see if subject is psychic
- clear all
- settings
- num_trials 5
- highest_number 3
- get info
- subject_name input('What is your name?', 's')
- for t 1num_trials
- trial set up
- random_num ceil(randhighest_number)
-
- perform experiment
- start_time GetSecs
5 disp('I am thinking of a number between')
disp(' 1 and ', num2str(highest_number))
response input('What is it?') stop_time
GetSecs response_time stop_time -
start_time is_correct (responserandom_nu
m) if(is_correct)
disp('Right!') else disp('Wrong!
The correct answer was ', ...
num2str(random_num)) end disp('Press
any key to continue') pause
disp('--------') record data
guessing_game_data(t, ) ... t,
response_time, response, ... random_num,
is_correct end
Task 1 Write this section of the
program Hint input without s is a good way To
get numeric responses from The subject
- guessing_game.m
- test to see if subject is psychic
- clear all
- settings
- num_trials 5
- highest_number 3
- get info
- subject_name input('What is your name?', 's')
- for t 1num_trials
- trial set up
- random_num ceil(randhighest_number)
-
- perform experiment
- start_time GetSecs
6 disp('I am thinking of a number between')
disp(' 1 and ', num2str(highest_number))
response input('What is it?') stop_time
GetSecs response_time stop_time -
start_time is_correct (responserandom_nu
m) if(is_correct)
disp('Right!') else disp('Wrong!
The correct answer was ', ...
num2str(random_num)) end disp('Press
any key to continue') pause
disp('--------') record data
guessing_game_data(t, ) ... t,
response_time, response, ... random_num,
is_correct end
- guessing_game.m
- test to see if subject is psychic
- clear all
- settings
- num_trials 5
- highest_number 3
- get info
- subject_name input('What is your name?', 's')
- for t 1num_trials
- trial set up
- random_num ceil(randhighest_number)
-
- perform experiment
- start_time GetSecs
7- guessing_game.m
- test to see if subject is psychic
- clear all
- settings
- num_trials 5
- highest_number 3
- get info
- subject_name input('What is your name?', 's')
- for t 1num_trials
- trial set up
- random_num ceil(randhighest_number)
-
- perform experiment
- start_time GetSecs
disp('I am thinking of a number between')
disp(' 1 and ', num2str(highest_number))
response input('What is it?') stop_time
GetSecs response_time stop_time -
start_time is_correct (responserandom_nu
m) if(is_correct)
disp('Right!') else disp('Wrong!
The correct answer was ', ...
num2str(random_num)) end disp('Press
any key to continue') pause
disp('--------') record data
guessing_game_data(t, ) ... t,
response_time, response, ... random_num,
is_correct end
8CSV format
- Stands for Comma separated value
- It is a simple text file for data storage
- Each data item is delimited (separated) by a
comma - Each data row is delimited by a return character
9Notepad example
1, 4, 9.3 1, 9, 100 4, 0, 12
Save as test.csv Double click -gt opens in excel
10csvwrite
- saves a 2d matrix as a csv file
- csvwrite(FILENAME, MATRIX_VAR)
- my_matrix 3, 5 1, 2
- csvwrite(my_data.csv, my_matrix)
11 disp('I am thinking of a number between')
disp(' 1 and ', num2str(highest_number))
response input('What is it?') stop_time
GetSecs response_time stop_time -
start_time is_correct (responserandom_nu
m) if(is_correct)
disp('Right!') else disp('Wrong!
The correct answer was ', ...
num2str(random_num)) end disp('Press
any key to continue') pause
disp('--------') record data
guessing_game_data(t, ) ... t,
response_time, response, ... random_num,
is_correct end
- guessing_game.m
- test to see if subject is psychic
- clear all
- settings
- num_trials 5
- highest_number 3
- get info
- subject_name input('What is your name?', 's')
- for t 1num_trials
- trial set up
- random_num ceil(randhighest_number)
-
- perform experiment
- start_time GetSecs
data_storage csvwrite(subject_name, '.csv',
guessing_game_data)
12csvread
- clear all
- my_matrix csvread(my_data.csv)
- can be used to load parameters at the beginning
of a program - can be used to load data to analyze through matlab
13cell arrays
- this_is_a_matrix_and_a_vector 5, 3, 9, 3
- and_so_is_this hello
- which_is_the_same_as h, e, l, l, o
14cell arrays
- cell array provides a storage mechanism for
dissimilar kinds of data - they are like a matrix where each element is any
other data type
example_cell_array cat, 3, 5, 9
zebra, 10,
3 9, 5, dog
cat 3
zebra dog
5 9
10 3
9 5
15cat 3
zebra dog
5 9
10 3
9 5
- a example_cell_array2, 3
- example_cell_array2, 1 a
- example_cell_array2, 2(2, 1)
16cell2csv
- Works just like csvwrite
- Will only work with simple cell arrays
- no numeric vectors or matrices
- data trial number, time, color,
response 1, 5.99, blue, 3
2, 4, green, 2 - 3, 55, yellow, 2
- cell2csv(somefile.csv, data)
- !Note cell2csv is not a built in Matlab
function. Download it herehttp//ruccs.rutgers.e
du/matlab_course/materials/summer_2010/class_7/cel
l2csv.m
17important note
- to add one data element to a cell array
- data5, 3 'yellow
- curly braces
- To add one row of data
- data(5, ) 4, 2.93, yellow, 4
- parenthesis
- data(trial1, ) trial, time, color, response
18Creating a vector of numbers
- This is similar to the way you create an
iteration for a for loop - x (020) ----gt x 0, 1, 20
- x (0.10.11) ---gt x 0.1, 0.2, , 1.0
- x (0220) ----gt x 0, 2, 4, 20
- x (0220) ---gt vertical vector like the
previous one
19discrete distributions and selecting segments
- you can use something like x(020) to specify a
discrete distribution of 21 different
possibilities - to select the first 6 elements you could sayy
x(16) ---gt y would be 0, 1 5 - Or to select the last 4 elements
- y x(end-3end) ---gt y would be 17, 18, 19, 20
20y binopdf(x(16), 5, .8)
- used to generate a binomial distribution function
the case in which one flips a weighted coin - .8 probability of getting heads
- there are 6 possibilities (0 heads, 1 head 5
heads - y gives the probability of each of these outcomes
This function requires the stats toolbox
21Plotting
- Subplot will create a figure window for plotting
- subplot (2, 1, 2) will say we are going to create
a (2 row, 1 column, ) figure. The last number
specifies that we are currently going to write in
the second element of this figure. - plot(y) will create a graph on currently chosen
section of the figure (specified by the third
parameter in subplot)
22setting up the plot
- xlim(1 5) sets the limits of the x axis
- bar(y) will use a bar graph
- ylabel(Probability of n)
- xlabel(N)
- title(Discrete distributions should be plotted
as histograms)
23more then one figure?
- figure opens a new figure window
- All new subplots will go to the new figure
- subplot(2, 1, 1)
- bar(y)
- xlim(1 21) we want 21 possibilities
24more then one graph on a plot
- hold on tells matlab to keep the previous graph
and draw a new one on top - stairs(cumsum(y), r)
- cumsum(y) lt--- each value is the sum of previous
values of y - r makes the graph line red
- stairs will plot it as a staircase
- hold off now any future graph additions will
clear previous graphs on this plot - legend(distribution, cumsum)
25ezplot Easy-to-use function plotter
- ezplot(fun) plots the expression fun(x) over the
default domain -2pi lt x lt 2pi. - This example plots the implicitly defined
function x2 - y4 0 over the domain -2pi,
2piezplot('x2-y4') - You can specify a different domain by passing in
a second argument to ezplot, in this case -6ltxlt6
and -2ltylt1eq'y - sin(x) 1/2' ezplot(eq,-6
,6,-2,1)
26Solving Equations Using Matlab (symbolic toolbox)
- http//people.clarkson.edu/wwilcox/ES100/eqsolve.
htm - The present tutorial deals exclusively with
numerical methods of solving algebraic and
trigonometric equations, both single and several
simultaneously, both linear and non-linear. - Analytical solutions can be obtained using the
methods described in the symbolic tutorial.
When both symbolic and numerical methods work,
sometimes one is simpler and sometimes the other.
27Solving Equations Using Matlab (examples)
- fzero command - finds the value of x for f(x)
0. e.g. For equation sin2(x) e-x/2 1 0x
fzero('sin(x)2exp(-x/2)-1', -1) - Using solve to solve linear and quadratic
equationse.g. For equation y x2 1, or y
5x - 4solve(x2-1)solve(5x 4) - And many more types of equations. Just read the
tutorial and the help page for solve, dsolve,
fsolve, fzero, etc.