Prices: 14.1 Course Example - Comparing Prices: (1/2) Comparing Gasoline Prices
Prices: 14.1 Course Example - Comparing Prices: (1/2) Comparing Gasoline Prices
Prices: 14.1 Course Example - Comparing Prices: (1/2) Comparing Gasoline Prices
iskeyword
Introduction
Introduction: User
Interaction Similarly, you can communicate the state of the program back to the user. For
One way to get the user's input is example, if the user enters a country which doesn't exist in the data set, you can
to explicitly ask the user to type display a custom error message.
one of the possible values.
The data is not available for the country entered.
The warning function will show the user the warning message you specify. warning('missing data')
warning('This is a warning!')
Warning: This is a warning!
TASK
Create a warning message with the text missing data.
The error function will show the user the error message you specify. error('bad data')
error('This is an error!')
This is an error!
TASK
Create an error message with the text bad data.
You can create dialog boxes that display messages, errors, or warnings. The corresponding functions
are msgbox, errordlg, and warndlg.
msgbox('Analysis complete')
errordlg('Data not found')
warndlg('Non-integer values rounded.')
Try making each of these dialog boxes.
Please close any open dialog boxes by clicking OK before moving on.
When you are finished practicing, please move to the next section.
14.3 Decision Branching: (1/10) Introduction
You can create a program that will respond to a user's input,
taking different actions based on that input.
x = rand;
if x > 0.5
y = 3; % Executed only if x > 0.5
end
TASK
Modify the script so that the variable B is defined as 1
only when A is greater than 1.
if A > 1
B=1
end
You may want to execute some other code if the condition TASK
is not met. To do this, you can use the else keyword. Try modifying the script so that when the if condition is
x = rand; not satisfied, the script will set the variable B to 0.
if x > 0.5
y = 3; if A > 1
else
y = 4;
B=1 ;
end else
B=0
end
14.3 Decision Branching: (6/10) Using if-elseif-else
TASK if income < 9275
Modify the script so that when income < 9275, the value rate =0.1
of rate = 0.10. Otherwise, rate = 0.15. else
rate = 0.15
end
If the condition after the if statement evaluates to false, TASK
you can use elseif to check another condition. Further modify the script so that:
Multiple elseif blocks may be added. If they all evaluate
to false, then the else block is evaluated. When income < 9275 ⇒ rate = 0.10
if condition When 9275 <= income < 37650 ⇒ rate =0.
code 15
elseif condition1
code When income >= 37650 ⇒ rate = 0.25
elseif condition2
code if income < 9275
else
code rate = 0.10
end elseif 9275<= income & income < 37650
rate = 0.15
else rate = 0.25
end
x = ones(13,1);
lx = length(x)
lx =
13
y = ones(1,21);
ly = length(y)
ly =
21
TASK LC = LENGTH(COUNTRIES)
Try creating a variable
named lc containing the length
of countries.
One option is to find the best fit line between each set of
countries.
Rather than typing out similar commands over and over, you
can use a for-loop to execute a block of code repeatedly.
14.5 For Loops: (8/9) Plotting Gasoline Prices for Each Country
TASK load gPrices
Currently, the script finds the
ctry = 'Japan';
linear fit between prices in Japan
and Canada. % Find the index value for ctry
idx = strcmp(ctry,countries);
Modify the code so that linear fits ctryPrices = prices(:,idx);
are found between Japan and all
the countries in countries.
for k = 1:length(countries)
c(k,:) = polyfit(ctryPrices,prices(:,k),1)
end