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

조회 수: 5 (최근 30일)
%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);

답변 (1개)

Nora Khaled
Nora Khaled 2020년 11월 22일
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
Hunter Fritsch
Hunter Fritsch 2020년 11월 22일
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];

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

카테고리

Help CenterFile Exchange에서 Oceanography and Hydrology에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by