% Properties of cell arrays >> letters = {'a', 'b', 'c', 'd', 'e'}; >> whos letters % Name Size Bytes Class % letters 1x5 310 cell array %Grand total is 10 elements using 310 bytes >> x = letters(2) %x = 'b' >> whos x % Name Size Bytes Class % x 1x1 62 cell array %Grand total is 2 elements using 62 bytes >> y = letters{2} %y = b >> whos y % Name Size Bytes Class % y 1x1 2 char array %Grand total is 1 element using 2 bytes %Construct a cell array piece by piece >> t{1,1} = 'upper left'; >> t{1,2} = 'upper right'; >> t{2,1} = 'lower left'; >> t{2,2} = 'lower right'; >> t %t = % 'upper left' 'upper right' % 'lower left' 'lower right' >> t{2,1} %ans = lower left >> [rows, cols] = size(t) %rows = 2 %cols = 2 >> t(1,:) %ans = 'upper left' 'upper right' select.m function selection = select(array) %SELECT - take a cell array, and randomly take %one element from the cell arrray to output. %random index index = floor(1 + rand*(length(array))); %eliminate the case when rand is one if (index == length(array) +1) index = length(array); end selection = array(index); >> letters = {'a', 'b', 'c', 'd', 'e'}; path(path, 'U:\lab04\'); for i = 1:10 x(i) = select(letters);end >> x %x = % Columns 1 through 7 % 'b' 'd' 'c' 'e' 'd' 'c' 'a' % Columns 8 through 10 % 'e' 'c' 'd' chooserow.m function row = chooserow(array) %CHOOOSEROW - Chooses one of the rows and returns % the cell array of the chosen row. [n, m] = size(array); %random index index = floor(1 + rand*(n)); %eliminate the case when rand is one if (index == n +1) index = n; end %grab the row of the index row = array(index,:); >>t{1,1} = 'upper left'; t{1,2} = 'upper right'; t{2,1} = 'lower left'; t{2,2} = 'lower right'; >> chooserow(t) %ans = % 'lower left' 'lower right' >> chooserow(t) %ans = % 'upper left' 'upper right' >> chooserow(t) %ans = % 'lower left' 'lower right' catlab4.m %CATLAB4 - the cat for lab 4 %initialize state state = 'happy'; %infinite loop while 1 %prompt user for input str = input('Enter absent, pet, feed, or time passes: ','s'); %quit if 'quit' or 'exit' if strcmp(str,'quit') break; end if strcmp(str,'exit') break; end %call possibleUpdate result = possibleUpdate(state, str); state = result{1}; %print output disp(result{2}) %quit if the cat dies if strcmp(state, 'dead') break; end end possibleUpdate.m function result = possibleUpdate( state, input ) %initialize the result, a stuttering output result = {state, 'absent'}; switch (state) case 'happy' switch(input) case 'pet' result = {state, 'purrs'}; case 'feed' result = {state, 'throws up'}; case 'time passes' result = {'hungry', 'rubs'}; end case 'hungry' switch(input) case 'pet' result = {state, 'bites'}; case 'feed' result = {'happy', 'purrs'}; case 'time passes' result = {'dead', 'dies'}; end case 'dead' result = {state, 'dies'}; end >> catlab4 Enter absent, pet, feed, or time passes: absent absent Enter absent, pet, feed, or time passes: pet purrs Enter absent, pet, feed, or time passes: feed throws up Enter absent, pet, feed, or time passes: time passes rubs Enter absent, pet, feed, or time passes: pet bites Enter absent, pet, feed, or time passes: feed purrs Enter absent, pet, feed, or time passes: time passes rubs Enter absent, pet, feed, or time passes: time passes dies % the one-line-change to catchooselab4.m to make the % state machine non-deterministic result = chooserow(possibleUpdate(state, str)); % the one-line-change to possibleUpdate.m to make the % state machine non-deterministic case 'feed' result = {'happy', 'purrs';... 'hungry', 'rubs'}; >> catchooselab4 Enter absent, pet, feed, or time passes: time passes rubs Enter absent, pet, feed, or time passes: feed purrs Enter absent, pet, feed, or time passes: time passes rubs Enter absent, pet, feed, or time passes: feed rubs Enter absent, pet, feed, or time passes: quit feedCat.m %FEEDCAT - cat state machine cascaded with the feeder %initialize feederState = 'happy'; catState = 'happy'; for i=1:10, [feederState,input] = feeder(feederState, '1'); result = chooserow(possibleChooseUpdate(catState, input)); catState = result{1}; disp(result{2}) end feeder.m function [nextState, output] = feeder (state, input) %feed when 1 or time passes if ~(strcmp(input, 'absent')) switch(state) case 'happy' output = 'time passes'; nextState = 'hungry'; otherwise output = 'feed'; nextState = 'happy'; end else nextState = state; output = 'absent'; end >> feedCat rubs purrs rubs purrs rubs purrs rubs rubs dies dies catImmortal.m %CATIMMORTAL - run the virtual cat finite state machine with a %feedback loop to ensure the cat does not die %initialize driverState = 'happy'; catState = 'happy'; for i=1:10 %feed or time passes base on the state in = feeder(driverState); %what the cat decided to do with feed or time passes catOut = chooserow(possibleChooseUpdate(catState, in)); catState = catOut{1}; out = catOut{2}; %driver state should be the same as the cat state driverState = catState; driverOut = feedbackUpdate(driverState, out); driverState = driverOut{1}; disp(out) end feeder.m %FEEDER - the output of the non-deterministic cat %(just like my cat at home) function out = feeder (state) switch(state) case 'hungry' out = 'feed'; otherwise out = 'time passes'; end feedbackUpdate.m %FEEDBACKUPDATE - the output of the update function is a pair of 1x2 cell array %with format [state, output] function out = feedbackUpdate(state,in) %initialize to a stuttering output out = {state, 'absent'}; if ~strcmp(in, 'absent') switch(state) case 'happy' if strcmp(in, 'purrs') out = {'happy', 'time passes'}; else out = {'hungry', 'time passes'}; end otherwise if strcmp(in, 'purrs') out = {'happy', 'feed'}; else out = {'hungry', 'feed'}; end end end >> catImmortal rubs purrs rubs rubs rubs purrs rubs purrs rubs purrs