Executing functions based on user input
조회 수: 2 (최근 30일)
이전 댓글 표시
Aaron Staszewski
2020년 8월 4일
Hi all,
I am trying to create a way for a user to type in a string of charactors that executes a funciton. I have written the following code to do so:
userString = input('What program would you like: ','s');
if userString == 'nice name one'
nastyFunctionOne;
elseif userString == 'nice name two but longer'
nastyFuncitonTwo;
else
fprintf('you had a typo');
end
function nastyFunctionOne
fprintf('the first funciton executed\n');
end
function nastyFuncitonTwo
fprintf('the second function executed\n');
end
The problem with this is that the first userString and the second userString do not have the same matrix dimensions, so when a user types in "nice name two but longer" errors fly up and 'nastyFuncitonTwo' does not execute.
How would I make it so that the matrix dimmensions agree and the second (or more) funciton will run?
Thanks
댓글 수: 0
채택된 답변
편집: Wilmer Alexander - Conferencista en Automatización y Robótica más influyente en Iberoamérica
2020년 8월 4일
clc
clear all
userString = input('What program would you like: ','s')
switch userString
case 'nice name one'
nastyFunctionOne;
case 'nice name two but longer'
nastyFuncitonTwo;
otherwise
fprintf('you had a typo');
end
function nastyFunctionOne
fprintf('the first funciton executed\n');
end
function nastyFuncitonTwo
fprintf('the second function executed\n');
end
댓글 수: 1
Thanks for asking. Please accept my proposal if your questions were resolved. I remain at your disposal for any questions.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Condensed Matter & Materials Physics에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!