I am trying to work out a volume given input values but it says theres an error ''Index must not exceed 1''?

1 view (last 30 days)
I am trying to work out volume of building using number of floors, width of floors, length of floors and height of floors but when it comes to working it out an error comes up about index exceeding number of array elements and says ins=dex must not exceed 1. Im not sure how to fix this.
n=1;
Building = input('Enter Building name: ','s');
while isempty(Building)
Building = input('Enter Building name: ','s');
end
sBuilding(n).bName =Building;
sBuilding(n).bNoOfFloors = input('Enter Number of Floors as an integer: ');
NumberOfFloors = sBuilding(n).bNoOfFloors;
sBuilding(n).bWidth = input('Enter the floors width as an integer: ');
WidthOfFloors = sBuilding(n).bWidth;
sBuilding(n).bLength = input('Enter the floors length as an integer: ');
LengthOfFloors = sBuilding(n).bLength;
sBuilding(n).bHeight = input('Enter the floors height as an integer: ');
HeightOfFloors = sBuilding(n).bHeight;
BuildingVolume= NumberOfFloors(WidthOfFloors*LengthOfFloors*HeightOfFloors);
disp('Building volume is:'+BuildingVolume)
disp("")

Accepted Answer

the cyclist
the cyclist on 3 May 2022
This expression:
NumberOfFloors(WidthOfFloors*LengthOfFloors*HeightOfFloors)
is trying to index into the variable NumberOfFloors.
Did you perhaps mean
NumberOfFloors*WidthOfFloors*LengthOfFloors*HeightOfFloors?
Also, you cannot concatenate character array and numbers with this syntax:
disp('Building volume is:'+BuildingVolume)
You can use a string instead:
disp("Building volume is:"+BuildingVolume)

More Answers (0)

Tags

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!