How to set a string as a CODE in script file?

Legend CODE
Sum SU
Difference DI
How can I set this code(SU and DI) so that the user will only input these codes and will work the other lines?

回答 (2 件)

Star Strider
Star Strider 2022 年 4 月 27 日

0 投票

I am not certain what you are actually asking.
If you want the user to enter specific values and assign them in the code to specific variables, I prefer using the inputdlg function. That gives the script control of the variable assignments.

2 件のコメント

Ben Jan
Ben Jan 2022 年 4 月 27 日
The user will pick a method from the list by typing the correct code. The program displays an error message when an invalid code is entered and prompts the user to re-enter the correct code.
Star Strider
Star Strider 2022 年 4 月 28 日
Use listdlg instead. That should work.

サインインしてコメントする。

Walter Roberson
Walter Roberson 2022 年 4 月 28 日

0 投票

validcodes = {'SU', 'DI'};
while true
choice = input('Enter a code: ', 's');
if strcmpi(choice, validcodes); break; end
fprintf('Bad code, try again\n');
end
But have you considered using menu() ?

質問済み:

2022 年 4 月 27 日

コメント済み:

2022 年 4 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by