Don't understand what 's' is

28 views (last 30 days)
Hunter Long
Hunter Long on 2 Sep 2019
Commented: Stephen23 on 2 Sep 2019
%Origami fortune teller
clc; clear;
color = input('Enter (R)ed, (G)reen, (B)lue or (Y)ellow: ', 's');
if color == 'R' || color == 'G' || color == 'B' || color == 'Y' %proceed
if color == 'R' || color == 'B'
num = input('Coose 1, 2, 5 or 6: ');
if num == 1
fprintf('Fortune 1 \n');
elseif num == 2 %fortune 2
elseif num ==5 %fortune 5
elseif num == 6 %fortune 6
else
fprintf('Invalid inpuit! \n')
end
else
num = input('Choose 3, 4, 7 and 8: ');
if num == 3
elseif num == 4
elseif num == 7
elseif num == 8
else
fprintf('Invalid Input! \n');
end
end
else
fprintf('Invalid input! \n');
end
This is an example code to demonstrate if/else statements and nested if/else statements, but I'm confused about what the 's' denotes in the color input. I'm attempting to complete a similar code, but it won't function with or without the 's' and I'm wondering if I'm doing something else incorrectly or if I'm misinterpreting this notation.
  1 Comment
Stephen23
Stephen23 on 2 Sep 2019
Note that if color is a scalar character you can simplify this
color == 'R' || color == 'G' || color == 'B' || color == 'Y'
using ismember:
ismember(color,'RGBY')
or using basic using logical operations:
any(color=='RGBY')

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 2 Sep 2019
Edited: Stephen23 on 2 Sep 2019
The input documentation describes the effect of its second optional input argument as "str = input(prompt,'s') returns the entered text, without evaluating the input as an expression."
Simply put:
  • sans 's': executes the input text, e.g. if you input 'x' it will return the value of the variable x.
  • with 's': returns the literal text, e.g. if you input 'x' it will return the character 'x'.
In general, evaluating arbitrary text provided by a user is not a good way to write code.

More Answers (0)

Categories

Find more on Data Type Identification in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!