制作表格新手入门matlab表格循环赋值,[初学笔记]matlab中的whileloop中使⽤
break语。。。
这个下午为了改这段代码改了⼀下午。纠结的点是:
1 为什么break语句不发挥作⽤,依然死循环
2 break发挥作⽤了,但只有⼀次重新输⼊机会
3 break⼀次重新输⼊后,gender的输出依然是第⼀个错误结果,没有被overwrite
⽤到的运算步骤必须放到循环⾥⾯,不然break语句⽆法执⾏
下⾯是我⾃⼰的代码,留意其中很重要的计算部分
cig2 = isempty(gender); % if it is an empty input
cig1 = strcmp(gender,s1); % compare the input and the right answer
judcig1 = any(cig1); % use any to test is it any "1" in the answer
以及重新赋值部分
gender = gender;
尤其是计算部分,如果在循环⾥⾯省略,是⽆法达到循环的,会⼀直死循环或者直接break
如果赋值部分不加上去,有可能会导致输出的结果永远都是错的结果
ingender = cell(1,3); % establish a cell
ingender = {"f","m","x"}; % 3 input strings into the cell
s1 = cellstr(ingender); % transfer from cell to string
%%% 可以写成function,⽤于循环中直接引⽤function,下⾯的部分也可以直接跑function,是否有其他⽅式?
gender = der; % transefer a struct into a string
cig2 = isempty(gender); % if it is an empty input
cig1 = strcmp(gender,s1); % compare the input and the right answer
judcig1 = any(cig1); % use any to test is it any "1" in the answer
while ((judcig1 == 0) || (cig2 == 1))
fprintf("\n\n Error! Invalid input!\n\nPlease enter ""f"" for female, ""m"" for male, ""x"" for third sex.\r\n"); % error der = input ("\n\n please enter your gender. (f/m/x)\n\n","s"); % input again
gender = der; % transefer a struct into a string % it"s necessary to be here
cig2 = isempty(gender); % if it is an empty input
cig1 = strcmp(gender,s1); % compare the input and the right answer
judcig1 = any(cig1); % use any to test is it any "1" in the answer
if (judcig1 == 1) % until the any is "1", end this loop
break; % break the while loop
end
gender = gender; % save the right answer end % end, when it"s the correct answer