How do I enter a Menu choice in a statement

I am creating a program that prompts a user to choose a choice from a menu and then use that option to insert in an "fprintf" Statement. Here is what I have so far, but as you can see in the bold statement below, it is missing the choice that was chosen by the user in the beginning.
clear
clc
close all
Materials={'Ash';'Hickory';'Maple';'Pine'};
Cost=[4.35 4.95 6.35 3.75]; % Estimated material cost to produce 25 bats from each material chosen
LECost=1.25; % Labor and Energy Cost regardless of material chosen.
BatMat= menu('Please select Bat material', Materials(1), Materials(2), Materials(3), Materials(4));
if BatMat == 1;
fprintf('Ash');
elseif BatMat ==2;
fprintf('Hickory');
elseif BatMat ==3;
fprintf('Maple');
else
fprintf('Pine');
end
BatPrice=input('Please enter selling price per bat of chosen material: $');
NoBats=input('Please enter the total number of bats the manufacturer can produce per week without an upgrade: ');
NoWeeks=input('Please enter the the number of weeks the manufacturer plans to run the bat production machinery in a year: ');
%Calculation
Ttlbats= (NoBats*NoWeeks);
input('Please enter number of ADDITIONAL bats that can be produced in a week with an upgrade: ');
Fixedcost=input('Please enter the fixed cost for the upgrade: $');
VariableCost=(Cost(1)+LECost);
fprintf('Selling price per bat:\t $%0.2f \n', BatPrice);
fprintf('Total Variable Cost per bat: \t $%0.2f \n', VariableCost);
fprintf('NO UPGRADE \n');
profit=(NoBats*BatPrice);
fprintf('Producing %0.0f _%s_ bats a week for %0.0f weeks = %0.0f total bats\n\t Profit \t\t$%0.0f\n' ,NoBats,BatMat,NoWeeks,Ttlbats,profit) %this line was marked

답변 (2개)

Walter Roberson
Walter Roberson 2017년 11월 10일

0 개 추천

Your code
BatMat= menu('Please select Bat material', Materials(1), Materials(2), Materials(3), Materials(4));
if BatMat == 1;
shows us that you know that the result of menu() is the item index. But
fprintf('Producing %0.0f _%s_ bats a week for %0.0f weeks = %0.0f total bats\n\t Profit \t\t$%0.0f\n' ,NoBats,BatMat,NoWeeks,Ttlbats,profit) %this line was marked
shows us that you now expect BatMat (the index) to be the string.
On that line you need to change BatMat to materials{BatMat}

댓글 수: 1

Juan Cervantes
Juan Cervantes 2017년 11월 10일
편집: Juan Cervantes 2017년 11월 10일
I ended up taking out the if statements and changed the line to materials{BatMat}, which worked perfectly! Thank you!

댓글을 달려면 로그인하십시오.

Swathi Kulkarni
Swathi Kulkarni 2020년 10월 6일

0 개 추천

what is the ouput of this ?please help
clear
clc
S=[2012,27,17;2011,24,13;2010,29,7];
fprintf(‘Clemson=USC Football Rivalry:\n\n’);
fprintf(‘Year\tUSC\t\tClesmon\n’);
fprintf(‘%0.0f\t%0.0f\t\t%00f\n’,S(1,1),S(1,2),S(1,3));
fprintf(‘%0.0f\t%0.0f\t\t%00f\n’,S(2,1),S(2,2),S(2,3));
fprintf(‘%0.0f\t%0.0f\t\t%00f\n’,S(3,1),S(3,2),S(3,3));

카테고리

도움말 센터File Exchange에서 Mathematics and Optimization에 대해 자세히 알아보기

태그

질문:

2017년 11월 10일

답변:

2020년 10월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by