I have a menu in which a user selects a continent and enters in a year that they want to look at a set of data values for. How would I go about extracting the values in matrix

1 vue (au cours des 30 derniers jours)
Code for continent and year selection below:
function [continent, con_data] = choose_continent
%prompt messaged displayed in command window
disp ('Choose a continent in the menu to view its fossil fuel consumption')
%menu to choose continent
continent = menu('Choose a contient', 'Africa', 'Asia', 'Europe', 'North America', 'Oceania', 'South and Central America');
%if a continet isn't selceted and menu closed then an error messaged is
%displayed and the menu is reopened
while continent == 0
disp('Error! Please selected a continent.');
continent = menu('Choose a contient', 'Africa', 'Asia', 'Europe', 'North America', 'Oceania', 'South and Central America');
end
%loads the corresponding data for the chosen continent
if continent == 1
continent = 'Africa';
con_data = xlsread('Africa.xlsx')
elseif continent == 2
continent = 'Asia';
con_data = xlsread('Asia.xlsx')
elseif continent == 3
continent = 'Europe';
con_data = xlsread('Europe.xlsx')
elseif continent == 4
continent = 'North America';
con_data = xlsread('NorthAmerica.xlsx')
elseif continent == 5
continent = 'Oceania';
con_data = xlsread('Oceania.xlsx')
elseif continent == 6
continent = 'South and Central America';
con_data = xlsread('SouthCentralAmerica.xlsx')
end
function year = choose_year_2
year = input('Enter a year to view: ','s');
if year < 1964
disp('Error! Please enter a year between 1965 and 2021')
year = input('Enter a year to view: ','s')
elseif year > 2021
disp('Error! Please enter a year between 1965 and 2021')
year = input('Enter a year to view: ','s')
else
disp('Thank you')
end
  3 commentaires
Voss
Voss le 15 Déc 2022
@Daniel Jackson: You don't want to use 's' in the call to input to get the year, since that is numeric:
function year = choose_year_2
year = input('Enter a year to view: ');
if year < 1964
disp('Error! Please enter a year between 1965 and 2021')
year = input('Enter a year to view: ')
elseif year > 2021
disp('Error! Please enter a year between 1965 and 2021')
year = input('Enter a year to view: ')
else
disp('Thank you')
end
Also, you should use input in a while loop to get and validate the year, like you do in choose_continent to get and validate the continent.
Voss
Voss le 15 Déc 2022
@Daniel Jackson: Here's a more concise code structure you can use for choose_continent:
function [continent, con_data] = choose_continent()
% prompt messaged displayed in command window
disp('Choose a continent in the menu to view its fossil fuel consumption');
% menu to choose continent
all_continents = {'Africa', 'Asia', 'Europe', 'North America', 'Oceania', 'South and Central America'};
while true
continent = menu('Choose a contient', all_continents{:});
if continent ~= 0
break
end
% if a continent isn't selected and menu closed then an error messaged is
% displayed and the menu is reopened
disp('Error! Please selected a continent.');
end
% store the continent name
continent = all_continents{continent};
% remove ' and ' and then remove ' ' (space) from continent,
% and append '.xlsx', to get the corresponding file name:
file_name = [strrep(strrep(continent,' and ',''),' ','') '.xlsx'];
% loads the corresponding data for the chosen continent
con_data = xlsread(file_name);

Connectez-vous pour commenter.

Réponse acceptée

Voss
Voss le 15 Déc 2022
% user selects continent and year:
[continent, con_data] = choose_continent();
year = choose_year_2();
% then store the row of con_data corresponding to the selected year:
data = con_data(con_data(:,1) == year,:)
  2 commentaires
Daniel Jackson
Daniel Jackson le 15 Déc 2022
Thank you very much, I can now see where I was going wrong, and as to why I was just going round in circles.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Software Development Tools dans Help Center et File Exchange

Produits


Version

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by