%% clear all the variables clear %% cut string x='ring'; x(1:3) %% ans = rin %% concatenate string x='ring'; y = 'the? z = 'bell? [x, y, z] %% ans = ringthebell %% cell array >> c = {'ring' 'offhook' 'end greeting' 'end message' 'absent'}; >> c(1) %% ans = 'ring' %% Present Working Directory pwd %% HELLO - Say hello. disp('Hello'); %% For-loop to count 'a' >> d = {'a' 'b' 'a' 'a' 'b'}; >> count = 0; >> for i = 1:length(d), if strcmp(d(i), 'a') count = count + 1; end; end; >> count %%count = 3 %%function with 1 argument function result = reverse(argument) % REVERSE - return the argument array reversed. result = argument(length(argument):-1:1); %% multi-variable function assignments function [result1, result2] = myfunction(arg1, arg2) >> [r1, r2] = myfunction(a1, a2); %% give the path to search for .m files >> path(path, 'U:\lab03\'); count.m function result = count(argument) % COUNT - count the number of character in a set. result = 0; for i = 1:length(argument), if strcmp(argument(i), 'a') result = result + 1; end; end; %% Count the number of 'a' from count.m path(path, 'U:\lab03\'); count({'a', 'b', 'c', 'a', 'aa'}) %% ans = 2 count(['a', 'b', 'c', 'a', 'aa']) %% ans = 4 countas.m % COUNTAS - Count the number of a’s in the input. while 1 response = input('Enter a string:','s'); if (strcmp(response,'quit') |... strcmp(response,'exit')) break; end disp(count(response)); end >> countas %Enter a string:hahah % 2 %Enter a string:quit >> update.m function [next, output] = update(state, input) %% implementation of the C4. State machine switch input case '0' next = 0; if state output = '1'; else output = '0'; end case '1' next = 1; if state output = '1'; else output = '0'; end otherwise next = state; output = 'absent'; end delay.m % DELAY - Just keep calling update state = 0; while 1 in = input('Enter 0, 1, absent, or quit: ', 's'); if (strcmp(in, 'quit') | strcmp(in, 'exit')) break; end [state, output] = update(state, in); disp(output); end %>> delay %Enter 0, 1, absent, or quit: 0 %0 %Enter 0, 1, absent, or quit: 1 %0 %Enter 0, 1, absent, or quit: 0 %1 %Enter 0, 1, absent, or quit: 1 %0 %Enter 0, 1, absent, or quit: 2 %absent %Enter 0, 1, absent, or quit: quit %catForever.m inputs = input('Input:', 's'); % initial state s = 'happy'; while ~strcmp(inputs, 'quit') && ~strcmp(inputs, 'exit') disp(['The Cat was "',s,'", before the "',feeder(s,inputs),'"']); [s, y] = update(s, feeder(s,inputs)); %display output and state disp(['The Cat "',y, '", it is going to be "',s,'"']); if strcmp(y, 'dies') break; end; inputs = input('Input:','s'); end; %feeder.m function [output] = feeder (state, input) %feed when 1 or time passes if ((strcmp(input, '1')||strcmp(input,'time passes'))... && strcmp(state, 'hungry') ) output='feed'; %time passes when input is 1 and state != hungry else if (strcmp(input, '1')) output='time passes'; %all other inputs are fine, let them be else output=input; end end % catForever Output >> catForever Input:1 The Cat was "happy", before the "time passes" The Cat "rubs", it is going to be "hungry" Input:1 The Cat was "hungry", before the "feed" The Cat "purrs", it is going to be "happy" Input:time passes The Cat was "happy", before the "time passes" The Cat "rubs", it is going to be "hungry" Input:time passes The Cat was "hungry", before the "feed" The Cat "purrs", it is going to be "happy" Input:time passes The Cat was "happy", before the "time passes" The Cat "rubs", it is going to be "hungry" Input:pet The Cat was "hungry", before the "pet" The Cat "bites", it is going to be "hungry" Input:quit