필터 지우기
필터 지우기

Arrays have incompatible sizes for this operation.

조회 수: 5 (최근 30일)
Enrico
Enrico 2023년 3월 12일
답변: Walter Roberson 2023년 3월 12일
OptSelection = input(' Will you like to Add Delete ','s');
while strcmpi(OptSelection,"Add") ~=1 && strcmp(OptSelection,"Delete") ~=1
fprintf(' You have selected a not existing option \n');
fprintf(' Make sure that the initials of the selected option are capital \n');
OptSelection = input(' Selected the correct option ','s');
end
fprintf('Print %s \n',OptSelection);
if OptSelection == 'Delete'
fprintf('Okay1');
else OptSelection == 'Add';
fprintf('Okay2');
end

채택된 답변

Walter Roberson
Walter Roberson 2023년 3월 12일
while strcmpi(OptSelection,"Add") ~=1 && strcmp(OptSelection,"Delete") ~=1
That is valid code, if a bit awkward. Less awkward would be something like
while ~ismember(lower(OptSeletion), ["add", "delete"])
then
if OptSelection == 'Delete'
Earlier you used "Delete" instead of 'Delete' and you used strcmpi() instead of == . The previous test was valid. But this test is comparing the character vector in OptSelection to the character vector 'Delete'. This is like coding if [65 100 100] == [68 101 108 101 116 101] -- you know it is going to error out because the two vectors are not the same length.
When you are comparing character vectors use strcmp() or strcmpi() .
Note that "Delete" is not a character vector, it is a scalar string() object. The == operation is defined for string objects and automatically takes different lengths into account; furthermore if you have a character vector == a string object, the character vector is automatically converted to a string object. So it would be valid to code
if OptSelection == "Delete"
... or you can use strcmp() or strcmpi()
Note that
fprintf(' Make sure that the initials of the selected option are capital \n');
the restriction to capitals is not necessary if you use strcmpi()
Have you considered using questdlg ?

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 String에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by