MATLAB switch case and try catch usage examples

Source: Internet
Author: User

%clear variable or directive clc;clear;%allows the user to enter parameter disp ('This function exercises the switch statement');d ISP ('enter 1-10 for one of the numbers, and the system determines the parity.'); Count= Input ('Enter a number:');%according to the number of inputsSwitch(count) Case{1,2,3,4,5}, disp ('the number you entered is odd.');  Case{2,4,6,8,Ten}, disp ('the number you entered is even.'); Otherwise disp ('the number you entered is out of range.'); End

It is important to note that:

1. After swith, do not enclose the colon case with {} instead of brackets ()!!!! You can add a comma after a statement

2. The use of break is unclear, the feeling adds and does not add all the same? What's going on

-----------------

%Defining Variables%Input: Count Count%clear variable or directive clc;clear;%allows the user to enter parameter disp ('This function exercises the Try-catch statement');d ISP ('Enter an index and the system takes out the elements in the array.');%initialize an array of arr=[1-3 2-4];Try%displays the corresponding subscript content in the array inder= Input ('Enter a number:'); Content=num2str (arr (inder)); Disp ('the contents of the array are:'); Disp (content);Catch%if error disp ('Try partial Code program error'); End

Note that any error in the try section causes the program to enter the catch section, not just the subscript, but the display error, etc.

MATLAB switch case and try catch usage examples

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.