Performing actions on a multiple choice list

function year = year_choose
year = listdlg('SelectionMode','multiple','PromptString',...
'Choose two Years','ListString',{'2017', '2018', '2019'});
promptMessage = sprintf(['Would you like to continue with ...' ...
'your chosen year? ,\nor Cancel to abort processing?']);
button = questdlg(promptMessage, 'Continue', 'Continue', 'Cancel', 'Continue');
if strcmp(button, 'Cancel')
return; % or break or whatever...
end
if year == 1 && 2
fprintf('You have chosen:\n2017');
elseif year == 2
fprintf('You have chosen:\n2018');
elseif year == 3
fprintf('You have chosen:\n2019');
end
end
Here is my code, so this code brings up a list for the user to select from, their are three value and the user is only going to
choose 2 of them however say for example they choose '2017 and 2018', it stores their answer as the value [1,2]. How do i then
reference their choice and use fprintf to display their choice to later use in a plot. I tried to do it in the 'if' statement
using the operand && but it gave me an error.

 채택된 답변

Walter Roberson
Walter Roberson 2021년 2월 7일

1 개 추천

if length(year) ~= 2
error('You choose the wrong number of years, should have chosen 2');
end
if ismember(1, year)
fprintf('You have chosen:\n2017\n');
end
if ismember(2, year)
fprintf('You have chosen:\n2018\n');
end
if ismember(3, year)
fprintf('You have chosen:\n2019\n');
end
selected_years = sort(year + 2017 - 1); %sort is probably not needed

댓글 수: 7

Thank you very much that really has helped.
Just another quick one, the selected_years has no been stored as a 1X2 matrix, so say for example the user chooses the value 2017 and 2018 from the list, the value of selected_years will now be [2017,2018] . How will i then use this to select values from an xlsx file that i have already loaded.
function [cube_time, cube_year] = choose_cube_time(cube_data,year)
if year == 1
cube_time = cube_data(1:12,1);
cube_year = cube_data(1:12,2);
elseif year == 2
cube_time = cube_data(13:24,1);
cube_year = cube_data(13:24,2);
elseif year == 3
cube_time = cube_data(25:36,1);
cube_year = cube_data(25:36,2);
end
end
This is my code which i used to select the data from the file when i had only a scalar
value for the year, but now ill be using the selected_years variable which will have
more than one value as mentioned above
function [cube_time, cube_year] = choose_cube_time(cube_data,year)
%assuming that year will now be a list of 2017, 2018, or 2019
%instead of the old 1, 2, 3 style:
yoff = reshape(year,1,[]) - 2017;
idx = reshape((1:12).' + yoff*12, 1, []);
cube_time = cube_data(idx,1);
cube_year = cube_data(idx,2);
Cizzxr
Cizzxr 2021년 2월 7일
Would you be able to explain what the first two lines of code is doing, starting 'yoff' so that i am able to implement it into my code without getting and errors because currently its giving me the error
'Index in position 1 is invalid. Array indices must be positive integers or logical values.'
Thank you
As I put in the comment:
%assuming that year will now be a list of 2017, 2018, or 2019
%instead of the old 1, 2, 3 style:
This reflects where you posted,
but now ill be using the selected_years variable
as the selected_years variables has the years in form 2017, 2018, or 2019, instead of codes like 1, 2, 3.
So, given 2017, 2018, 2019, then subtracting 2017 gives you 0, 1, or 2. Multiply that by 12 gives 0, 12, or 24. Add (1:12).' to that gives columns 1:12 or 13:24 or 25:36 . reshape(, 1, []) makes a row vector out of all of the rows and columns. For example, if the input was 2017 2019 then
(1:12).' + yoff*12
would be
1 25
2 26
3 27
4 28
5 29
6 30
7 31
8 32
9 33
10 34
11 35
12 36
and reshape to a row vector would give you [1 2 3 4 5 6 7 8 9 10 11 12 25 26 27 28 29 30 31 32 33 34 35 36]
The row vector of indices is then used to select rows from cube_data, so the above-listed rows would be retrieved all at the same time .
To be explicit, this code expects that you will pass in a vector of years with multiple entries, and it will return the appropriate year and time data for all of the years listed, as one big column for each of the two variables. The code would be perfectly happy to be passed only one year, but it does not expect it. It would be easy to change the code so that it returned one column for each selected year: just skip the reshape() step for the idx construction.
Chances are that you did not notice my comment saying that you are to use the year numbers and that is why you got indexing errors.
Cizzxr
Cizzxr 2021년 2월 7일
Thats really helpful, ive now used this to create a plot however because my x values are repeating numbers from 1-12, on the plot theres a line joining the two years together, how would i split these up on the plot and maybe add a legend to the trendline to show the two different years the user has selected
try
idx = (1:12).' + yoff*12;
without the reshape
Cizzxr
Cizzxr 2021년 2월 7일
Hi Walter,
Thank you for your help today i really appreciate it, have a great week!
Ciaran

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

릴리스

R2020b

태그

질문:

2021년 2월 7일

댓글:

2021년 2월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by