% Outstanding Homework Lesson 4 Importing data
% Author: Romulo Lollato.
% Date: 18-Jul-2013

%% Importing data and naming the variables

A = importdata('countryAreaPop.xlsx'); %imports the excel data into MatLab

%% Isolate variables
% Agarea=A(:,1);

% this did not work because the A(:,1) gets the whole excel table.
% however when imported to MatLab it divided into data and text, so you
% need to specify you want A.data (remember Samuel.phone)

Agarea=A.data(:,1); %this specify that on the A.data file derived from your original excel
                    %the first colum, all rows, will be considered agarea
Pop2011=A.data(:,2); %this specify that on the A.data file derived from your original excel
                    %the second colum, all rows, will be considered Pop2011
Pop2050=A.data(:,3); %this specify that on the A.data file derived from your original excel
                    %the third colum, all rows, will be considered Pop2050
%Now you have your three variables defined and you can use it however you
%want
%% Substituting missing values
popindex2011=Pop2011==-999;
%Pop2011(Pop2011==-999)=nan; %for this dataset all the numbers that are -999 will be replaced with nan

Pop2011(2,1)=196700000; %substitutes the number in row 2 col 1 from Pop2011 by 196700000 (population in Brazil)
Pop2011(5,1)=1241000000; %substitutes the number in row 2 col 1 from Pop2011 by 1241000000 (population in India)
Pop2011(8,1)=313900000; %substitutes the number in row 2 col 1 from Pop2011 by 313900000 (population in USA)

%% Practice examples
%i.e.

% finding the minimal agricultural area per capita
Ratio=Agarea./Pop2011; %calculates the ratio agarea/population
[minratio,rowrat]=min(Ratio); %selects the minimum ratio value, here called "minratio", which will be in the row "rowrat"
Minratio=A.textdata{rowrat+1,1}; %creates a variable Minratio that will be a text with the name of the country with minimum ratio on the ".textdata" file
disp(['The country with less agricultural area per capita in 2011 is ',Minratio]) %writes this followed by the name of the country as an output

% finding maximum percent population increase
Increase=(Pop2050-Pop2011)./Pop2011; %calculates the percent increase in population by 2050 with reference to 2011
[maxincrease,rowinc]=max(Increase); %selects the maximum increase, here called "maxincrease", which will be in the row "rowinc"
Maxincrease=A.textdata{rowinc+1,1}; %creates a variable Maxincrease that will be a text with the name of the country with maximum increase on the ".textdata" file
disp(['The country with greater percent population increase by 2050 is ',Maxincrease]) %writes this followed by the name of the country as an output

% finding the minimum ratio of agricultural area by population in 2050
Ratio2050=Agarea./Pop2050; %calculates the ratio of agricultural area per capita in 2050
[minratio50,rowrat50]=min(Ratio2050); %selects the minimum ratio value, here called "minratio50", which will be in the row "rowrat50"
Minratio50=A.textdata{rowrat50+1,1}; %creates a variable Minratio50 that will be a text with the name of the country with minimum ratio on the ".textdata" file
disp(['The country with less agricultural area per capita in 2050 is ',Minratio50]) %writes this followed by the name of the country as an output

%% finding the maximum values
% Maximum area
[Maxarea,rowarea]=max(Agarea); %creates a variable maxarea that is the max value in Agarea, brings from which row it came from as "rowarea", from the function max in the colum (Agarea)
Largestcountry=A.textdata{rowarea+1,1}; %creates a variable Largestcountry that will be a text with the name of the country with max area on the ".textdata" file
disp(['The country with the largest area is ',Largestcountry])%writes this followed by the name of the country as an output

% Maximum population 2011
[MaxPop2011,rowpop]=max(Pop2011); % creates a variable MaxPop2011 that is the max value in Pop2011, brings from which row it came from as "rowpop", from the function max in the colum (Pop2011)
Highestpop=A.textdata{rowpop+1,1}; % creates a variable Highestpop that will be a text with the name of the country with max population on the ".textdata" file
disp(['The country with the greatest population in 2011 is ',Highestpop]) %writes this followed by the name of the country as an output

% Maximum population 2050
[MaxPop2050,rowpop50]=max(Pop2050);
Highestpop50=A.textdata{rowpop50+1,1}; %here we search for {rowpop50+1,1} because there is an extra line in the textdata files (the header) than there is in the data only file. Thus we add one.
disp (['The country with the greatest population in 2050 is ',Highestpop50])





                   