Matlab menu sum values

3 ビュー (過去 30 日間)
Eliska Paulikova
Eliska Paulikova 2022 年 12 月 3 日
編集済み: Image Analyst 2022 年 12 月 3 日
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

回答 (2 件)

Voss
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)))

Image Analyst
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

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

タグ

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by