using menu() function to calculate time differences

1 view (last 30 days)
london = datetime
timeZones = {'Amsterdam', 'Tokyo', 'Canberra', 'Los Angeles', 'New York', 'Local (London)','Close'}
while button~=7 %7th button is 'close'
button == menu('Choose your timezones',timeZones)
%Amsterdam
if button ==1
ams=london+hours(1)
disp(ams)
disp('+1:00')
%Tokyo
elseif button ==2
tok=london+hours(9)
disp(tok)
disp('+9:00')
%Canberra
elseif button ==3
can= london +hours(11)
disp(can)
disp('+11:00')
%Los Angeles
elseif button==4
los = london +hours(-8)
disp(los)
disp('-8:00')
%New York
elseif button==5
new= london +hours(-5)
disp(new)
disp('-5:00')
%London(Local)
elseif button==6
disp('No time difference')
disp(london)
else
end
end
I used a while loop so the menu doesn't close after a choice.
it does work in that sense, but when i press 'close' i cannot close it and the loop becomes endless unless i force to end. Also no matter what button i press, it gives london time.
what have I done wrong here?

Answers (1)

Sreelakshmi S.B
Sreelakshmi S.B on 6 Mar 2019
There's a mistake in the 5th line of you code.You're using == .You need to use =.
Also,you need to have the first assignment to 'button' before comparing it in the while condition.The code below works fine.I just made a few modifications to your code:
london = datetime;
timeZones = {'Amsterdam', 'Tokyo', 'Canberra', 'Los Angeles', 'New York', 'Local (London)','Close'};
button = menu('Choose your timezones',timeZones);
while button~=7 %7th button is 'close'
%Amsterdam
if button ==1
ams=london+hours(1);
disp(ams);
disp('+1:00');
%Tokyo
elseif button ==2
tok=london+hours(9);
disp(tok);
disp('+9:00');
%Canberra
elseif button ==3
can= london +hours(11);
disp(can);
disp('+11:00');
%Los Angeles
elseif button==4
los = london +hours(-8);
disp(los);
disp('-8:00');
%New York
elseif button==5
new= london +hours(-5);
disp(new);
disp('-5:00');
%London(Local)
elseif button==6
disp('No time difference');
disp(london);
else
end
button = menu('Choose your timezones',timeZones);
end

Categories

Find more on Oceanography and Hydrology in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!