Accepting Multiple String Inputs

Hey! so i'm trying to condense some code i wrote yesterday into something more compact. the first step is just running a while loop that runs a program i made (called stuff.m) if the statement is true. but i want it to be able to accept a big selection of inputs, it's not happy with my choice of "strcmp(elmnt,'fine','good'))". any ideas?
thanks!
elmnt = input('how are you? ','s');
while (strcmp(elmnt,'fine','good'))
run stuff
end

 採用された回答

Iain
Iain 2013 年 6 月 7 日

0 投票

use strcmpi instead, if you want to make sure your user will type those strings as stated. put your "accepted" strings into a cell array:
Accepted = {'fine','good','great','fantastic','fan-dabby-dozy'};
while any(strcmpi(elmnt, Accepted))
run stuff
end
Look up strfind/findstr/regexp to see if theres a better way of doing what you want to.

3 件のコメント

kenny
kenny 2013 年 6 月 7 日
編集済み: kenny 2013 年 6 月 7 日
it doesn't like the while line but creating the array and the strcmpi were nice catches- thanks. i'll try making this work
Iain
Iain 2013 年 6 月 7 日
Did you use "{" and "}" on the accepted line?
kenny
kenny 2013 年 6 月 7 日
oh! i'm an idiot and misspelled "accepted". i really appreciate your help, thank you.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

質問済み:

2013 年 6 月 7 日

Community Treasure Hunt

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

Start Hunting!

Translated by