How to create a user defined function that contain if else function. Can I create a user function from the line e line %user defined function
이전 댓글 표시
%menu for user to choose
disp('Convert between Celcious and Fahrenheit');
disp('Choose a number:');
disp('1.Celcious to Fahrenheit');
disp('2.Fahrenheit to Celcious');
%input
choice = input("\nInput:",'s');
while (strcmpi(choice, '1') == 0 && strcmpi(choice, '2') == 0)
disp('Choose number 1 or 2');
choice = input("\nInput: ",'s');
end
%user defined function
if (choice == '1')
cel = input("\nEnter temperature in Celcious to convert to Ferenheit:");
while (cel < 0)
disp('Enter a positive number, please');
cel = input("\nEnter temperature in Celcious to convert to Ferenheit:");
end
if (cel >= 0)
cel2fa = (cel*1.8) + 32;
fprintf('Coverted: %.2f Fahrenheit \n',cel2fa);
end
end
if (choice == '2')
fa = input('\nEnter temperature in Ferenheit to convert to Celcious:');
while (fa < 0)
disp('Enter a positive number, please');
fa = input("\nEnter temperature in Celcious to convert to Ferenheit:");
end
if (fa >= 0)
fa2cel = (fa-32)/1.8;
fprintf('Coverted: %.2f Celcious \n',fa2cel);
end
end
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!