disp(' syntax and structure ...'); syntax and structure ... disp(' if while for switch try'); if while for switch try disp(' else end catch otherwise case elseif'); else end catch otherwise case elseif disp('other keywords: function '); other keywords: function disp(' commands (functions) : sprintf, disp, length (see website for a list'); commands (functions) : sprintf, disp, length (see website for a list disp(' terminology: program script function'); terminology: program script function % create a variable that has the sin and cos a = [sin(3), cos(3)] a = 0.1411 -0.9900 edit pc5.m type pc5_fake.m % Fake pc5 to make an example function d = pc5(n) if (n == 1) d = 'Sunday'; end edit pc5.m dayname {Undefined function or variable 'dayname'. } day_of_the_week {Undefined function or variable 'day_of_the_week'. } ver ---------------------------------------------------------------------------------------------------- MATLAB Version: 8.3.0.532 (R2014a) MATLAB License Number: 542128 Operating System: Mac OS X Version: 10.9.5 Build: 13F1911 Java Version: Java 1.7.0_11-b21 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode ---------------------------------------------------------------------------------------------------- MATLAB Version 8.3 (R2014a) Simulink Version 8.3 (R2014a) Bioinformatics Toolbox Version 4.4 (R2014a) Control System Toolbox Version 9.7 (R2014a) Curve Fitting Toolbox Version 3.4.1 (R2014a) DSP System Toolbox Version 8.6 (R2014a) Financial Toolbox Version 5.3 (R2014a) Fuzzy Logic Toolbox Version 2.2.19 (R2014a) Global Optimization Toolbox Version 3.2.5 (R2014a) Image Processing Toolbox Version 9.0 (R2014a) Instrument Control Toolbox Version 3.5 (R2014a) MATLAB Coder Version 2.6 (R2014a) MATLAB Compiler Version 5.1 (R2014a) Neural Network Toolbox Version 8.2 (R2014a) Optimization Toolbox Version 7.0 (R2014a) Parallel Computing Toolbox Version 6.4 (R2014a) Partial Differential Equation Toolbox Version 1.4 (R2014a) Signal Processing Toolbox Version 6.21 (R2014a) SimMechanics Version 4.4 (R2014a) Simscape Version 3.11 (R2014a) Simulink Coder Version 8.6 (R2014a) Stateflow Version 8.3 (R2014a) Statistics Toolbox Version 9.0 (R2014a) Symbolic Math Toolbox Version 6.0 (R2014a) Wavelet Toolbox Version 4.13 (R2014a) disp('Error invalid day number'); Error invalid day number error('Error invalid day number'); {Error invalid day number } day_of_the_week = 34 day_of_the_week = 34 sprintf('day %d ', day_of_the_week); sprintf('day %d ', day_of_the_week) ans = day 34 pc5(1) ans = Sunday pc5(2) ans = Monday d = pc5(3); sprintf('The %d day of the week is called %s.', 3, d) ans = The 3 day of the week is called Tuesday. for 0:8 for 0:8 | {Error: Unexpected MATLAB expression. } for d = 0:8 name = pc5(d); disp(sprintf('The %d day of the week is called %s.', d, name)); end The 0 day of the week is called day 0 . The 1 day of the week is called Sunday. The 2 day of the week is called Monday. The 3 day of the week is called Tuesday. The 4 day of the week is called Wednesday. The 5 day of the week is called Thursday. The 6 day of the week is called Friday. The 7 day of the week is called Saturday. The 8 day of the week is called day 8 . disp(sprintf('The %d day of the week is called %s.', d, pc(d))) {Undefined function 'pc' for input arguments of type 'double'. } disp(sprintf('The %d day of the week is called %s.', d, pc5(d))) The 8 day of the week is called day 8 . d = 4 d = 4 disp(sprintf('The %d day of the week is called %s.', d, pc5(d))) The 4 day of the week is called Wednesday. disp(sprintf('The %d day of the week is called %s.', ... d, pc5(d))) d, pc5(d))) The 4 day of the week is called Wednesday. disp(sprintf('The %d day of the week is called %s.', ... d, pc5(d))) The 4 day of the week is called Wednesday. type pc5.m % pc5 to convert a number to a string % name, date, class, usage... function dayname = pc5(day_of_the_week) % Given the day_of_the_week map it to a name. switch (day_of_the_week) case 1 dayname = 'Sunday'; case 2 dayname = 'Monday'; case 3 dayname = 'Tuesday'; case 4 dayname = 'Wednesday'; case 5 dayname = 'Thursday'; case 6 dayname = 'Friday'; case 7 dayname = 'Saturday'; otherwise % disp('Error invalid day number'); % error('Error invalid day number'); % dayname = 'Error invalid day number'; % dayname = 'day'; dayname = sprintf('day %d ', day_of_the_week); end % end disp('this is output'); this is output retvalue = pc5(3); f = 5 - 2 f = 3 pi ans = 3.1416 -pi:.1:pi ans = Columns 1 through 6 -3.1416 -3.0416 -2.9416 -2.8416 -2.7416 -2.6416 Columns 7 through 12 -2.5416 -2.4416 -2.3416 -2.2416 -2.1416 -2.0416 Columns 13 through 18 -1.9416 -1.8416 -1.7416 -1.6416 -1.5416 -1.4416 Columns 19 through 24 -1.3416 -1.2416 -1.1416 -1.0416 -0.9416 -0.8416 Columns 25 through 30 -0.7416 -0.6416 -0.5416 -0.4416 -0.3416 -0.2416 Columns 31 through 36 -0.1416 -0.0416 0.0584 0.1584 0.2584 0.3584 Columns 37 through 42 0.4584 0.5584 0.6584 0.7584 0.8584 0.9584 Columns 43 through 48 1.0584 1.1584 1.2584 1.3584 1.4584 1.5584 Columns 49 through 54 1.6584 1.7584 1.8584 1.9584 2.0584 2.1584 Columns 55 through 60 2.2584 2.3584 2.4584 2.5584 2.6584 2.7584 Columns 61 through 63 2.8584 2.9584 3.0584 -pi:.1:3*pi ans = Columns 1 through 6 -3.1416 -3.0416 -2.9416 -2.8416 -2.7416 -2.6416 Columns 7 through 12 -2.5416 -2.4416 -2.3416 -2.2416 -2.1416 -2.0416 Columns 13 through 18 -1.9416 -1.8416 -1.7416 -1.6416 -1.5416 -1.4416 Columns 19 through 24 -1.3416 -1.2416 -1.1416 -1.0416 -0.9416 -0.8416 Columns 25 through 30 -0.7416 -0.6416 -0.5416 -0.4416 -0.3416 -0.2416 Columns 31 through 36 -0.1416 -0.0416 0.0584 0.1584 0.2584 0.3584 Columns 37 through 42 0.4584 0.5584 0.6584 0.7584 0.8584 0.9584 Columns 43 through 48 1.0584 1.1584 1.2584 1.3584 1.4584 1.5584 Columns 49 through 54 1.6584 1.7584 1.8584 1.9584 2.0584 2.1584 Columns 55 through 60 2.2584 2.3584 2.4584 2.5584 2.6584 2.7584 Columns 61 through 66 2.8584 2.9584 3.0584 3.1584 3.2584 3.3584 Columns 67 through 72 3.4584 3.5584 3.6584 3.7584 3.8584 3.9584 Columns 73 through 78 4.0584 4.1584 4.2584 4.3584 4.4584 4.5584 Columns 79 through 84 4.6584 4.7584 4.8584 4.9584 5.0584 5.1584 Columns 85 through 90 5.2584 5.3584 5.4584 5.5584 5.6584 5.7584 Columns 91 through 96 5.8584 5.9584 6.0584 6.1584 6.2584 6.3584 Columns 97 through 102 6.4584 6.5584 6.6584 6.7584 6.8584 6.9584 Columns 103 through 108 7.0584 7.1584 7.2584 7.3584 7.4584 7.5584 Columns 109 through 114 7.6584 7.7584 7.8584 7.9584 8.0584 8.1584 Columns 115 through 120 8.2584 8.3584 8.4584 8.5584 8.6584 8.7584 Columns 121 through 126 8.8584 8.9584 9.0584 9.1584 9.2584 9.3584 plot(-pi:.1:3*pi) plot(cos(-pi:.1:3*pi)) for(9:-1:8) for(9:-1:8) | {Error: Unexpected MATLAB expression. } for 9:-1:8 for 9:-1:8 | {Error: Unexpected MATLAB expression. } for i=9:-1:8 disp(i) end 9 8 disp(i) 8 clear disp(i) 0.0000 + 1.0000i pi ans = 3.1416 format long pi ans = 3.141592653589793 while i=9:-.25:8 while i=9:-.25:8 | {Error: The expression to the left of the equals sign is not a valid target for an assignment. } while i >= 8 i = 9; disp(i); i = i - .25; end disp(i) 0.000000000000000 + 1.000000000000000i i = 9; while i >= 8 disp(i) i = i - .25; end 9 8.750000000000000 8.500000000000000 8.250000000000000 8 i = 9; while i >= 8 disp(i); i = i - .25; end 9 8.750000000000000 8.500000000000000 8.250000000000000 8 if (i > 4) disp('i gt 4'); else disp('i lt 4'); end i gt 4 diary off