Adding quotes to a user input

5 次查看(过去 30 天)
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 个评论
Jan
Jan 2011-9-30
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.

请先登录,再进行评论。

采纳的回答

Daniel Shub
Daniel Shub 2011-9-30
Try the 's' option.
method = input('linear or cubic? ', 's');

更多回答(3 个)

Wayne King
Wayne King 2011-9-30
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 2011-9-30
method = input('linear or cubic? ', 's');
will force the variable "method" to be a string.

Bashiru Bukari
Bashiru Bukari 2020-3-22
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

类别

Help CenterFile Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by