How do I fix this error to output the correct data? I get an error when I use disp.

1 view (last 30 days)
%Take user input for the weights (kg) of all fish from the haul as a vector
fishWeight = input('Enter the weights of the fish with [ ] around and a space between the values.');
%Use a loop to determine the total weight for each grade of fish in the haul, and the total income for each grade of fish
wholeFishWeight = 0;
wholeFishIncome = 0;
filletFishWeight = 0;
filletFishIncome = 0;
canningFishWeight = 0;
canningFishIncome = 0;
petFoodFishWeight = 0;
petFoodFishIncome = 0;
fishExtractsFishWeight = 0;
fishExtractsFishIncome = 0;
for(i = 1 : length(fishWeight))
if(fishWeight(i)>=30)
wholeFishWeight = wholeFishWeight + fishWeight(i)
wholeFishIncome = wholeFishIncome + 22*fishWeight(i)
elseif(fishWeight(i)>=20)
filletFishWeight = filletFishWeight + fishWeight(i)
filletFishIncome = filletFishIncome + 16*fishWeight(i)
elseif(fishWeight(i)>=10)
canningFishWeight = canningFishWeight + fishWeight(i)
canningFishIncome = canningFishIncome + 5*fishWeight(i)
elseif(fishWeight(i)>=5)
petFoodFishWeight = petFoodFishWeight + fishWeight(i)
petFoodFishIncome = petFoodFishIncome + 3*fishWeight(i)
else
fishExtractsFishWeight = fishExtractsFishWeight + fishWeight(i)
fishExtractsFishIncome = fishExtractsFishIncome + 1*fishWeight(i)
end
end
%Determine the total income resulting from selling the haul
totalIncome = wholeFishIncome + filletFishIncome + canningFishIncome + petFoodFishIncome + fishExtractsFishIncome;
%Output a detailed invoice including the weight of each grade of fish, the total income for each grade of fish, and the total income from the entire haul
disp('You caught ' + wholeFishWeight + ' kg of fish for Whole Fish, to be sold for $' + wholeFishIncome)
disp('You caught ' + filletFishWeight + ' kg of fish for Fillet, to be sold for $' + filletFishIncome);
disp('You caught ' + canningFishWeight + ' kg of fish for Canning, to be sold for $' + canningFishIncome);
disp('You caught ' + petFoodFishWeight + ' kg of fish for Pet Food, to be sold for $' + petFoodFishIncome);
disp('You caught ' + fishExtractsFishWeight + ' kg of fish for Fish Extracts, to be sold for $' + fishExtractsFishIncome);
disp('Your total income for this haul is $' + totalIncome);

Answers (1)

Nora Khaled
Nora Khaled on 22 Nov 2020
if the error in "disp", use
or maybe this works
X=['You caught ' , wholeFishWeight , ' kg of fish for Whole Fish, to be sold for $' , wholeFishIncome];
disp(X)
  1 Comment
Hunter Fritsch
Hunter Fritsch on 22 Nov 2020
I tried that but I stil get this error message
Error using +
Matrix dimensions must agree.
Error in fhunter_C4_P2 (line 40)
X=['You caught ' + wholeFishWeight + ' kg of fish for Whole Fish, to be sold for $'
, wholeFishIncome];

Sign in to comment.

Categories

Find more on Variables 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!