Is it possible to write some code that will add single quotations to a user input to make it into a string so that it can be used in a switch loop, etc.
For example:
method = input('linear or cubic? ');
switch(method)
case 'linear'
disp('Method is linear')
case 'cubic'
disp('Method is cubic')
end
The user here must type in 'linear' or 'cubic', is it possible to make it so they can just type in linear or cubic without quotes.

1 Kommentar

Jan
Jan am 30 Sep. 2011
You can find the solution also by: "help input". Using "help" and "doc" is a good idea whenever a problem with a specific command appears.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Daniel Shub
Daniel Shub am 30 Sep. 2011

1 Stimme

Try the 's' option.
method = input('linear or cubic? ', 's');

Weitere Antworten (3)

Wayne King
Wayne King am 30 Sep. 2011

1 Stimme

Hi, You can just use the 's' option in input.
method = input('linear or cubic?\n','s');
switch(method)
case 'linear'
disp('Method is linear')
case 'cubic'
disp('Method is cubic')
end
Robert Cumming
Robert Cumming am 30 Sep. 2011

0 Stimmen

method = input('linear or cubic? ', 's');
will force the variable "method" to be a string.
Bashiru Bukari
Bashiru Bukari am 22 Mär. 2020

0 Stimmen

Hi you can try this...
method =input('linear or cubic:','s');
switch method
case{'linear'}
disp('method is linear');
case{'cubic'}
disp('method is cubic');
end

Kategorien

Community Treasure Hunt

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

Start Hunting!

Translated by