1 / 14

Cells

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.

Download Presentation

Cells

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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 = {[]}

  2. Switching from cells to matrices and reading data from excel • Two very useful commands: num2cell(matrix) and cell2mat(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.

  3. 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 xlsread.. can try using csvread instead, but only works for reading in numbers. Otherwise, may have to use something like textscan, or save() and load() for .mat files.

  4. 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 1x7 cell array for column headers. Columns should be subject number, block 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.

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

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

  7. pracindex = []; usable = [{'Cal'}; {'Prac1'}; {'Prac2'}]; 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 end practimes = cell2mat(raw(pracindex,7)); averagepractime = mean(practimes); holding = cell2mat(raw(2:end,5)); accindex = find(holding == 4)+1; accscores = cell2mat(raw(accindex,6)); averageacc = mean(accscores); holding = cell2mat(raw(2:end,3)); timeindex = find(holding == 5)+1; times = cell2mat(raw(timeindex,7)); averagetime = mean(times);

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

  9. Structures Can also convert from cells to structures, or vice versa S = cell2struct(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 25x7 cell array, and dim was 2, the result would be a 25x1 struct with 7 fields. struct2cell is the complementary function for structures.

  10. Exercise 3 Modify your code from Exercise 2, so that it: loads data from all 3 subjects. Creates a 1x3 structure, alldata, to hold the subjects’ data Should have fields: subnum data (imported from excel) (from exc 2) acc4 time5

  11. alldata = repmat(struct('subnum', [], 'data', [], 'acc4', [], 'time5', []), 3,1); subs = 19:21; for n = 1:3 subn = subs(n); filename = ['sub', int2str(subn), '.xls']; [num, txt, raw] = xlsread(filename); alldata(n,1).subnum = subn; alldata(n,1).data = raw; end

  12. Exercise 3b 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.

  13. Using cell2struct subdata = []; for n = 1:3 filename = ['sub', int2str(subn(n)), '.xls']; [num, txt, raw] = xlsread(filename); subdata = [subdata; raw(2:end,:)]; end fulldata = cell2struct(subdata, raw(1,:), 2);

  14. 3b 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', int2str(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 end

More Related