1 首先熟悉 strcmp 这个函数
tf =
strcmp
(s1,s2)
输入
s1 =
'upon';
s2 = { 'Once', 'upon';
'a', 'time'};
tf = strcmp(s1,s2)
s2 = { 'Once', 'upon';
'a', 'time'};
tf = strcmp(s1,s2)
会得到
tf =
2×2 logical array
0 1
0 0
0 1
0 0
2 好的下面是 all 和 any的用法,详情自己看这个blog
转自 http://www.matlabsky.com/thread-4150-1-1.html
3 用上 struct,以及在循环 while loop里面,减少逻辑运算符号的使用
我们可以这样做
下面是我自己写的代码,亲测可以用
you.gender = input ('\n\n please enter your gender. (f/m/x)\n\n','s');
input1 = 'f';input2 = 'm';input3 = 'x'; % define the input answer
s1 = {input1,input2,input3}; % 3 input strings into the cell
tf = strcmp(you.gender,s1); % compare the input and the right answer
while any (tf) == 0 % if no correct answer(1),then input again
fprintf('\n\n Error! Invalid input!\n\nPlease enter ''f'' for female, ''m'' for male, ''x'' for third sex.\r\n');
you.gender = input ('\n\n please enter your gender. (f/m/x)\n\n','s');
end
input1 = 'f';input2 = 'm';input3 = 'x'; % define the input answer
s1 = {input1,input2,input3}; % 3 input strings into the cell
tf = strcmp(you.gender,s1); % compare the input and the right answer
while any (tf) == 0 % if no correct answer(1),then input again
fprintf('\n\n Error! Invalid input!\n\nPlease enter ''f'' for female, ''m'' for male, ''x'' for third sex.\r\n');
you.gender = input ('\n\n please enter your gender. (f/m/x)\n\n','s');
end