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 CenterFile Exchange에서 Whos에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by