Performing actions on a multiple choice list

조회 수: 1 (최근 30일)
Cizzxr
Cizzxr 2021년 2월 7일
댓글: Cizzxr 2021년 2월 7일
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일
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
Walter Roberson
Walter Roberson 2021년 2월 7일
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개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by