Cells Cells are pretty cool Can hold anything

  • Slides: 14
Download presentation
Cells • Cells are pretty cool. – Can hold anything (strings, a matrix, even

Cells • Cells are pretty cool. – Can hold anything (strings, a matrix, even other cells) – initialized like you would use zeros: X = cell(25, 1) to create a single, empty cell: X = {[]}

Switching from cells to matrices and reading data from excel • Two very useful

Switching from cells to matrices and reading data from excel • Two very useful commands: num 2 cell(matrix) and cell 2 mat(cell array). These convert matrices into cell arrays of the same size, and vice versa. • Often needed: some functions, such as find, do not work on cell arrays.

Reading from Excel • [num txt raw] = xlsread(‘filename’) is a way to read

Reading from Excel • [num txt raw] = xlsread(‘filename’) is a way to read in data from excel files • num is a matrix containing all cells with numbers • txt is a cell array containing all cells with strings • raw is a cell array containing all cells with either A note: Macs may have trouble with can try using csvread instead, but reading in numbers. Otherwise, may something like textscan, or save(). mat files. xlsread. . only works for have to use and load() for

Exercise 1 • Analyzing a simple dataset (example. xls). This data comes from a

Exercise 1 • Analyzing a simple dataset (example. xls). This data comes from a working memory task, where the secondary task is solving math equations. Included is accuracy, blocknum (what list they’re on), difficulty, and time to complete. • To begin, set up a 1 x 7 cell array for column headers. Columns should be subject number, block size, number in block, difficulty, accuracy, and time. • Next, read in example. xls, and concatenate these headers with the array containing just the numbers. It should now be the same size and have (almost) the same contents as the cell array.

[numbers, txt, raw] = xlsread('example. xls'); headers = [{'SUBJNO'} {'BLOCKNUM'} {'BLOCKSIZE'} {'NUMINBLOCK'} {'DIFFIC'} {'MATHACC'}

[numbers, txt, raw] = xlsread('example. xls'); headers = [{'SUBJNO'} {'BLOCKNUM'} {'BLOCKSIZE'} {'NUMINBLOCK'} {'DIFFIC'} {'MATHACC'} {'MATHTIME'}]; data = num 2 cell(numbers); data = [headers; data];

Exercise 2 • With the raw cell array, find: • Average time to complete

Exercise 2 • With the raw cell array, find: • Average time to complete the practice items (blocks Cal, Prac 1, and Prac 2) • Average Accuracy for Difficulty 4 • Average time to complete items in blocks of size 5

pracindex = []; usable = [{'Cal'}; {'Prac 1'}; {'Prac 2'}]; for n = 1:

pracindex = []; usable = [{'Cal'}; {'Prac 1'}; {'Prac 2'}]; for n = 1: length(raw) block = char(raw{n, 2}); if ismember(block, usable) %If the string block matches any of the three prac block names pracindex = [pracindex, n]; end practimes = cell 2 mat(raw(pracindex, 7)); averagepractime = mean(practimes); holding = cell 2 mat(raw(2: end, 5)); accindex = find(holding == 4)+1; accscores = cell 2 mat(raw(accindex, 6)); averageacc = mean(accscores); holding = cell 2 mat(raw(2: end, 3)); timeindex = find(holding == 5)+1; times = cell 2 mat(raw(timeindex, 7)); averagetime = mean(times);

Structures • Two common ways to initialize data = repmat(struct(‘field', val, ‘field', val), rows,

Structures • Two common ways to initialize data = repmat(struct(‘field', val, ‘field', val), rows, cols) data(n) = struct(‘field', val, ‘field', val)

Structures Can also convert from cells to structures, or vice versa S = cell

Structures Can also convert from cells to structures, or vice versa S = cell 2 struct(C, fields, dim) converts a cell matrix, C, into a structure S with field names specified by the cell array of strings, fields. dim specifies what dimension of C you are folding/collapsing into fields for S. If C were a 25 x 7 cell array, and dim was 2, the result would be a 25 x 1 struct with 7 fields. struct 2 cell is the complementary function for structures.

Exercise 3 Modify your code from Exercise 2, so that it: loads data from

Exercise 3 Modify your code from Exercise 2, so that it: loads data from all 3 subjects. Creates a 1 x 3 structure, alldata, to hold the subjects’ data Should have fields: subnum data (imported from excel) (from exc 2) acc 4 time 5

alldata = repmat(struct('subnum', [], 'data', [], 'acc 4', [], 'time 5', []), 3, 1);

alldata = repmat(struct('subnum', [], 'data', [], 'acc 4', [], 'time 5', []), 3, 1); subs = 19: 21; for n = 1: 3 subn = subs(n); filename = ['sub', int 2 str(subn), '. xls']; [num, txt, raw] = xlsread(filename); alldata(n, 1). subnum = subn; alldata(n, 1). data = raw; end

Exercise 3 b Create a second (N by 1) structure, fulldata, that concatenates all

Exercise 3 b Create a second (N by 1) structure, fulldata, that concatenates all 3 subjects’ data with a field for each column. Last, convert this structure into one large N by 7 cell array.

Using cell 2 struct subdata = []; for n = 1: 3 filename =

Using cell 2 struct subdata = []; for n = 1: 3 filename = ['sub', int 2 str(subn(n)), '. xls']; [num, txt, raw] = xlsread(filename); subdata = [subdata; raw(2: end, : )]; end fulldata = cell 2 struct(subdata, raw(1, : ), 2);

3 b with two for loops lengthfull = length(num) * 3; % raw has

3 b with two for loops lengthfull = length(num) * 3; % raw has headers, so use num fulldata = repmat(struct('SUBJNO', [], 'BLOCKNUM', [], 'BLOCKSIZE', [], 'NUMINBLOCK', [], 'DIFFIC', [], 'MATHACC', [], 'MATHTIME', []), lengthfull, 1); index = 1; for n = 1: 3 filename = ['sub', int 2 str(subn(n)), '. xls']; [num, txt, raw] = xlsread(filename); for o = 2: length(raw) fulldata(index). SUBJNO = raw{o, 1}; fulldata(index). BLOCKNUM = raw{o, 2}; fulldata(index). BLOCKSIZE = raw{o, 3}; fulldata(index). NUMINBLOCK = raw{o, 4}; fulldata(index). DIFFIC = raw{o, 5}; fulldata(index). MATHACC = raw{o, 6}; fulldata(index). MATHTIME = raw{o, 7}; index = index+1; end