フィルターのクリア

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 Center および File ExchangeString についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by