Posted By:
20 Mar, 2013 6:46 pm PDT

Using if else, elseif, and switch case in Matlab

I want to execute a script in which,

  • if a=1, one piece of code is implemented
  • if a = 2, 2nd piece of code is implemented,
  • if a is neither 1 nor 2, then 3rd piece of code is implemented

Thanks

Attachment:None


the syntax to use if-else statements are:

>>a = 1; % defining a varible

>>if(a==1)

disp('1st case')

elseif(a==2)

disp('2nd case')

else

disp('Other case')

end

 

Switch statements are:

 >>switch a
    case 1
        disp('1st case')
    case 2
        disp('2nd case')
    otherwise
        disp('Other case')
 end

% switch statement characters name ='John'; switch name case {'John' 'Dan'} disp('1st case') case ('Alex') disp('2nd case') otherwise disp('Other case') end
Attachment: if_switch.m
Was it useful?  




Please login to reply to this problem.