Matlab_Class_7

Download Report

Transcript Matlab_Class_7

Introduction To Matlab
Class 7
Instructors: Hristiyan (Chris) Kourtev
and Xiaotao Su, PhD
Optional Presentation Title
cell arrays
• cell arrays are a different data type
• 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’
‘zebra’
Unit Name
3
5
10 3
9 5
‘dog’
9
Optional Presentation Title
‘cat’
‘zebra’
3
5
10 3
9 5
• a = example_cell_array{2, 3}
• example_cell_array{2, 1} = a
• example_cell_array{2, 2}(2, 1)
Unit Name
‘dog’
9
Optional Presentation Title
cell2csv
• 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 here:
http://ruccs.rutgers.edu/matlab_course/materials/class_7/cell2csv.m
Unit Name
Optional Presentation Title
important note
• to add one data element to a cell array
– data{5, 3} = 'yellow’
• curly braces
• To add one row of data
– data(5, :) = {4, 2.93, ‘yellow’, 4}
• parenthesis
– data(trial+1, :) = {trial, time, color, response}
Unit Name
Optional Presentation Title
%guessing_game.m
%test to see if subject is psychic
clear all;
response = input('What is it?');
stop_time = GetSecs;
response_time = stop_time - start_time;
is_correct = (response==random_num);
%settings
num_trials = 5;
highest_number = 3;
if(is_correct)
disp('Right!');
else
disp(['Wrong! The correct answer was ', ...
num2str(random_num)]);
end
disp('Press any key to continue');
pause
disp('--------');
%get info
subject_name = input('What is your name?', 's');
for t = 1:num_trials
%trial set up
random_num = ceil(rand*highest_number);
%perform experiment
start_time = GetSecs;
disp('I am thinking of a number between');
disp([' 1 and ', num2str(highest_number)]);
Unit Name
%record data
guessing_game_data(t, :) =...
[t, response_time, response, ...
random_num, is_correct ];
end
%data_storage
csvwrite([subject_name, '.csv'], …
guessing_game_data);
Optional Presentation Title
%guessing_game.m
%test to see if subject is psychic
clear all;
%set up labels
response = input('What is it?');
stop_time = GetSecs;
response_time = stop_time - start_time;
is_correct = (response==random_num);
guessing_game_data = {‘trial’, ‘response time’, …
%settings
‘response’, ‘random_num’, ‘is_correct?’}
if(is_correct)
num_trials = 5;
highest_number = 3;
disp('Right!');
else
disp(['Wrong! The correct answer was ', ...
num2str(random_num)]);
%get info
end
subject_name = input('What is
your name?',
%record
data 's');
guessing_game_data(t+1,disp('Press
:) = … any key to continue');
pause
for t = 1:num_trials
{t, response_time, response,
random_num, is_correct}
disp('--------');
%trial set up
random_num = ceil(rand*highest_number);
%record data
guessing_game_data(t, :) =...
%perform experiment
[t, response_time, response, ...
start_time = GetSecs;
random_num, is_correct ];
disp('I am thinking of a number between');
end
disp([' 1 and ', num2str(highest_number)]);
%data_storage
Unit Name
cell2csv
csvwrite([subject_name, '.csv'], …
guessing_game_data);
Optional Presentation Title
Task - color differentiation (part 1)
1.
2.
Take subject name
screen_setup
–
trial loop (start with 5)
color = [rand*255, rand*255, rand*255]
–
put circles 1-4 on the screen
• one of the four circles will be slightly off (start with +/-50), color from
the others on one of the three color dimensions (randomly chose
dimension); careful that your deviation doesn’t go >255 or <0
• The subject will click on the circle they feel is different
• record data (whatever would be useful)
• if the subject was right in the last loop, the amount the color is off is
cut in half, otherwise, increase by half
3.
save trial data to csv
Unit Name
tip: make a function to determine if the cursor is in a shape
function is_in = in_shape(vect, mouse_x, mouse_y)
Optional Presentation Title
Task - color differentiation (part 2)
Further additions:
1. allow the subject to press 1 - 4 to select a circle (using
KbCheck)
2. put numbers in the circles
Hint: You need to use the following commands:
Screen('TextFont', window, fontName*);
Screen('TextSize', window, fontSize**);
Screen('DrawText', window, textString, x, y, textColor);
*fontName can be “Arial”, “Verdana” or other
*fontSize can be any number. Start with around 20 and adjust as needed
Unit Name