% Class 3
% Coded by Nigel Reuel on 1.22.2018
% More Matlab review
clc
close all
%
% Dice question
Val = randi(6); % This only generates integers
Val2 = ceil(rand()*6);
% Conditional Operators (compare values)
%{
NY = input('How many years have you been at ISU?');
Age = input('How old are you?');
if NY > 5
disp('Whoa. Time to hurry.')
elseif NY > 3 && Age > 30
disp('Time to REALLy hurry.')
elseif NY < 0 || Age < 12
disp('ERROR. That is not possible.')
else
disp('You are normal.')
end
% List of the conditional opperators
% > < == ~= (not equal) - look at table 3.2
%}
%{
% Example of finding array elements that meet a specific condition
% (see handout)
A = randi(15,5);
B = A<7;
% How do we extract the values?
A(B);
% If we want to know location, use the find function
Ind = find(A<7);
% Example with weather data
% Load the data and determine on what days of MAY 2017, was the temperature
% above 80'F?
D = xlsread('weather');
Ind = find(D(:,2)>=80);
HotDays = D(Ind,1);
% Catching errors
C = input('How many hours per day do you sleep?');
if C>24
warning('Too many hours in a day! Try again.')
return
end
B = 12;
% How to catch matlab errors
A = [2 4; 5 6];
B = [1; 4; 7];
try
C = A*B % This will attempt the operation
catch
% If matlab throws an error, have it do this
display('You cannot multiply these together')
end
%}
% Loops in Matlab
% for loop - use when you have a known # of iterations
Nthrows = 10;
SumVal = 0;
for i = 1:Nthrows
ThrowVal = randi(6);
SumVal = SumVal + ThrowVal;
end
% Let's use fprintf to display the value in a nice sentence
fprintf('The total value from your %d throws is equal to %d.\n',Nthrows,SumVal)
% Use a while loop is you do NOT know the number
% of iterations, but have some condition to exit
SumVal2 = 0
numThrows = 0;
while SumVal2 < 80
numThrows = numThrows + 1;
ThrowVal = randi(6);
SumVal2 = SumVal2 + ThrowVal;
end
display('This is the number of throws:')
numThrows
% Pause command
% If you need your code to wait between calculations
pause(1);
% If you want to track the time it takes for your code to run
tic
B = pi^300;
toc
% Dr. Reuel uses 'i' as an index, if for some
% reason you want to have imaginary number
1i; % this is the imaginary number
% Don't write over default variables
%
% Example of fprintf with decimal place
fprintf('I can remember pi as %.10f. I am smart.\n',pi)
% Exporting data
Data = [1 2 3; 4 5 6; 7 8 9];
%xlswrite('MyData',Data)
% Basics of plotting
% Example of plotting with discrete data
D = xlsread('weather');
HighVec = D(:,2); % This is the max temp
LowVec = D(:,4);
Dates = D(:,1);
plot(Dates,HighVec,'ro',Dates,LowVec,'k+:')
% We need to add labels and change to data points
xlabel('Date in May 2017')
ylabel('Temperature (°F)')
% How to generate a plot from a function
figure % this opens a new figure file, won't overide your first plot
F1 = @(x) log(x);
fplot(F1,[1 100])
%
% End of day example - game of Yahtzee
DV = randi(6,5,1)
CV = DV == DV(1,1)
YC = sum(CV)
if YC == 5
disp('YAHTZEE! YOU WIN')
else
disp('Try again.')
end