Ive got a simple enough problem code has to make sure user input is correct but Ive made a very ugly while loop and its not working at all probably needs to be completely redone, any tips are appreciated
if true
InputString = input('Input:', 's');
lenghtInput = length(InputString);
test = 'UuGgCcAa';
i = 1;
j = 1;
c = 1;
correctInput = false;
while correctInput ~= true
if InputString(i) ~= test(j) || j == lenghtInput
++j;
++c;
display(i)
if c == 3
c = 0;
i = i + 1;
end
end
correctInput = true;
end
%code
%it needs to check the only characters used are in the test variable

댓글 수: 4

riley collins
riley collins 2018년 5월 7일
i also know that I'm using too many counters as you can probably see
Walter Roberson
Walter Roberson 2018년 5월 7일
++j is not valid MATLAB syntax.
Why not just use strcmp() or strcmpi() ?
riley collins
riley collins 2018년 5월 7일
편집: riley collins 2018년 5월 7일
i should have explained better, the code can contain any pattern or sequence but i need to make sure it only contains "ugca" and their upper case counter parts and ++j isnt syntax uhg im getting languages mixed up again i think....
KSSV
KSSV 2018년 5월 7일
If you want to compare strings have a look on strcmp and strcmpi.

댓글을 달려면 로그인하십시오.

 채택된 답변

Walter Roberson
Walter Roberson 2018년 5월 7일

0 개 추천

tf = ismember(InputString, 'UuGgCcAa');
correctInput = all(tf);

댓글 수: 4

riley collins
riley collins 2018년 5월 7일
편집: riley collins 2018년 5월 7일
could you please elaborate on the uses of "tf"?
riley collins
riley collins 2018년 5월 7일
just to clarify this dose work thank you so much just trying to learn a bit
tf is an arbitrary variable name for this purpose. It could have been written as
are_individual_letters_correct = ismember(InputString, 'UuGgCcAa');
correctInput = all(are_individual_letters_correct);
The letters that are wrong would be
bad_letters = InputString(~are_individual_letters_correct);
ismember() checks each vector element in InputString against every element of 'UuGgCcAa' and returns true in the places that match and false in the places that do not match. If everything comes out as true then the entire string is valid.
riley collins
riley collins 2018년 5월 9일
thanks i wasnt sure

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by