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 次查看(过去 30 天)
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 个评论
Voss
Voss 2022-12-15
@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 2022-12-15
@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);

请先登录,再进行评论。

采纳的回答

Voss
Voss 2022-12-15
% 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,:)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by