% Class 3 % Coded by Nigel Reuel % Today is MATLAB Bootcamp Part 3 % Conditionals, Dispay, Exporting Data, Loops % Plotting, Yahtzee Game % A = 10; B = 6; C = 9; if A > 10 disp('A is greater than 10!') elseif A == 10 disp('A is equal to 10') else disp('A must be less than 10') end % Other logic statments % >, <, >=, <=, ~= (not equal) % You can use multiple conditions in the same line % %{ D = input('What is the value of D?'); if D > 10 && mod(D,2) == 0 disp('D must be greater than 10 and even!') elseif D < 10 || mod(D,2) == 0 % example of or disp('D is less than 10 or even') end %} % Catching errors M1 = [1 2 3; 4 5 6; 7 8 9]; M2 = [1 2; 3 4]; try Answer = M1*M2 catch warning('This math operator does not work!') end % Recall 'disp' can display to the command prompt % Using fprintf has more options % Table 3.1 in notes has the different options % Most common fprintf format is '%.4f' % This integrates numbers and text nicely % Example p = pi; %fprintf('Pi with 30 decimal spaces is %.30f',p) % Move command prompt to the next line fprintf('Pi with 30 decimal spaces is %.30f\n',p) % You can use multiple numbers nR = 5; nB = 3; nG = 7; fprintf('There are %d red, %d blue, and %d green items.\n',nR,nB,nG) % Exporting Data M = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16]; M2 = rand(5); % Export these numbers to excel % vs 2019 Matlab use writematrix writematrix(M2,'DataExport1.xlsx') % You can also record data that is mixed % (numbers and strings) % Use writecell M3 = {'Room' '# Students' 'chairs'; 23 50 25; 25 67 43} % Use write cell writecell(M3,'Data2.xlsx') % One example with xlswrite xlswrite('Data3',M3) % Loops in Matlab % for loop - know the number of steps your algorithm should take % while loop - don't know # of steps, but have some condition % % Examples %{ % We want the user to give us a number % We will square number and repeat 6 times N = input('Give a number') % Now we want to record the value of this number % on each iteration % Preallocate memory for the answers Avec = zeros(6,1); for i = 1:6 N = N*2; Avec(i,1) = N; end Avec fprintf('Final number is %d\n',N) % Example of while loop % User give number, divide by 2 % End when the number is less than 0.0001 N2 = input('Give a number') % Let's count the number of times the loop executes count = 0; while N2 > 0.0001 N2 = N2/2; count = count + 1; end fprintf('Code ran for %d iterations.\n',count) % Within a for or while loop you can also use % break - it exits the current loop % continue - keep executing lines in loop, but exit on next iteration % return - completely exit the function %} %{ % Example of infinite loop N3 = input('give number') while N3 > 5 N3 = N3 + 2 end %} % Remember you can use ctrl+c to exit operation % pause - command you can use to pause the program % % tic toc command, used to time lines of code n4 = 12; tic for i = 1:300 n4 = n4 + n4/2; end toc % A few notes on conditional statements % Let's do this with the weather data D = readmatrix('weather.xlsx'); HT = D(3:end,2) % How many days were > 80'C LA = HT>80 % Creates a logic array NumDays = sum(LA) % On what days was it > 80'C I = find(HT>80) % YAHTZEE example!