Matlab menu sum values
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
Voss
2022-12-3
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 个评论
Image Analyst
2022-12-3
编辑:Image Analyst
2022-12-3
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!