Matlab menu sum values
3 views (last 30 days)
Show older comments
Hello everyone,
I have a values in the menu, now I would like to choose for example two of them, and sum them. But if I read values from menu, it gives me 1 - 4 not -10000,3000 and so on ...
Please help
Thank you
clc,clear,close
n=input("Počet pohybů na účtu: ")
for i=1:n
msg = ("Vyber hodnotu: ");
vyber = ["-10000" "3000" "15000" "4000" "-6000"];
choice = menu(msg,vyber);
end
0 Comments
Answers (2)
Voss
on 3 Dec 2022
clc
msg = "Vyber hodnotu: ";
vyber = ["-10000" "3000" "15000" "4000" "-6000"];
n = input("Počet pohybů na účtu: ");
choice = zeros(1,n); % vector of user-selections
for i = 1:n
choice(i) = menu(msg,vyber); % store the i-th selection
end
% index the selections in vyber, convert to numbers, and sum:
result = sum(str2double(vyber(choice)))
0 Comments
Image Analyst
on 3 Dec 2022
Edited: Image Analyst
on 3 Dec 2022
Try this:
clc;
clear;
n=input("Počet pohybů na účtu: ")
msg = ("Vyber hodnotu: ");
vyber = ["-10000" "3000" "15000" "4000" "-6000", "Quit"];
theSum = 0;
for k = 1 : n
choice = menu(msg, vyber);
if choice == numel(vyber)
% User clicked Quit
break;
end
selectedVyber = vyber(choice);
% Sum it in.
theSum = theSum + str2double(selectedVyber);
fprintf('You selected button #%d which is %s. The sum so far is %d.\n', ...
choice, selectedVyber, theSum)
end
0 Comments
See Also
Categories
Find more on Whos 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!