%Martin Kennelly %class on 2-5-07 %today: Switches, formatted output, reading into very basic files. %Switches works in place of if's, ifelses, if your data range for the decision is a finite set. %Let's suppose we have, x=input('entre your value')%if (x==3), disp('hello') %elseif (x==2), disp('thanks') %elseif (x==1), disp('goodbye') %does not work for a switch as x>2, x>1, x>3 because this is not a finite set %lets do an example x=input('enter value') switch(x) case 1 disp('hello') case 2 disp ('thanks') end %now lets do a problem that takes in one unit as an input, and produces another %unit. we're going to go from english to metric. x=input('enter the number ') y=input('enter the type of unit that you want to convert from ') %we have never done an ifelse with strings, but we will now, allowing an %input of in, ft, yd, or mi switch(y) case'in' disp('you have ') x*25.4 disp('mm') %with a switch, it ends up using string compare behind the scenes case 'ft' disp('you have ') x*.305 disp('m') %now lets do formateed output %[%d - integer], [%e - scientific] fprintf(format, data) fprintf [%d - integer], [%e - scientific] %basically, we could have done this exact thing with ifelse, but it %would have been a lot more time consuming, and not nearly as neat %we could also do nested cases, which means that inside of out case %we could put a switch. %load filename, and it puts it into a matrix. %needs to have the same number of integers in it. ex. a 3 by 3 %matrix. First index is for the row, and the second index is for %the column. %if you want to save something in a format so that it can vbe rad %by something other thatn matlab, save it, and place,'-ascii' at %the end, and without quotes. x=input('enter the number ') y=input('enter the type of unit that you want to convert from ') switch(y) case'in' fprintf('you have %f mm\n',x*25.4') case 'ft' fprintf('you have %f meters\n',x*0.305') end x=input('enter the number ') y=input('enter the type of unit that you want to convert from ') switch(y) case'in' fprintf('Your %f inches is %f millimeters.\n',x,x*25.4) case 'ft' fprintf('Your %f feet is %f meters\n',x,x*0.305) end