does an "if does not contain operator" exist

35 ビュー (過去 30 日間)
steve guy
steve guy 2013 年 3 月 16 日
I am trying to write a function where the input must be entered in radians. My idea was to make the function return if the input does not contain "pi". Is there a way to do this?
ex. if you wanted to enter 90 you have to enter pi/2
Thanks
  2 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 3 月 16 日
It's not clear for me
steve guy
steve guy 2013 年 3 月 16 日
I edited it. hope it is more clear now

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

採用された回答

Jan
Jan 2013 年 3 月 16 日
編集済み: Jan 2013 年 3 月 17 日
Trust the users of your program:
d = input('Input angle in radians [0 <= x <= 2*pi]: ')
Then even "pi/2" is a valid answer.
Testing the input to contain the string 'pi' would be not sufficient: E.g. the string '32 pineappletrees' should not be accepted, but '3.14159265358979 / 2' should.
  3 件のコメント
Image Analyst
Image Analyst 2013 年 3 月 17 日
Of course. Why wouldn't it be allowed?
Jan
Jan 2013 年 3 月 18 日
@Steve: Yes, of course. You can call input() within a function also. But I'd prefer inputdlg.

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

その他の回答 (3 件)

Matt Kindig
Matt Kindig 2013 年 3 月 16 日
The "if does not contain" operation can be done with ismember, using something like:
notPi = ~ismember( user_input, pi)
However, due to numerical precision, you probably want to use a tolerance to determine how close the user is to pi, e.g.
notPi = abs(user_input-pi) > 1e-7

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 3 月 16 日
If the input does not contain pi does not mean that the input is not in radian.
Now if you want the input to contain pi
s=[]
while isempty(s)
u=input('Enter value (like 2*pi)','s')
s=strfind(u,'pi')
end
u=str2num(u)

Image Analyst
Image Analyst 2013 年 3 月 16 日
編集済み: Image Analyst 2013 年 3 月 16 日
Every number contains pi - some factor times it. So what you want is to have the user enter a string and see if "pi" is in the string and alert them if it's not in the string. Try this:
% Ask user to enter some string containing pi.
% Keep asking until they do.
defaultValue = 42;
titleBar = 'Enter a value';
userPrompt = 'Enter the value containing pi:';
while true
% Ask user for a number containing pi.
caUserInput = inputdlg(userPrompt, titleBar, 1, {num2str(defaultValue)});
if isempty(caUserInput),break,end; % Bail out if they clicked Cancel.
% Convert to lower case, then see if "pi" is in there.
userString = char(lower(caUserInput{1}));
piLocation = strfind(userString, 'pi');
if piLocation >= 1
% String contains pi so we can exit the loop now.
break;
end
end
  1 件のコメント
Greg Heath
Greg Heath 2013 年 3 月 18 日
Why not change the input to a normalized angle in [ 0 1) translated as radianangle/2/pi?

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

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by