Adding quotes to a user input

조회 수: 5 (최근 30일)
Peter
Peter 2011년 9월 30일
답변: Bashiru Bukari 2020년 3월 22일
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