MATLAB: Don’t understand what ‘s’ is

%selseif statement

%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.

Best Answer

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.