Zeros error when entering matrix data

3 vues (au cours des 30 derniers jours)
Tobias Weymouth
Tobias Weymouth le 12 Avr 2022
Commenté : Tobias Weymouth le 12 Avr 2022
Hi, I'm new to MATLAB for university and have got stuck on a part of code with a zeros error. Perhaps I should be using something different to zeros?
I'm entering floor dimensions and then room dimensions with the long run intention to develop the code further to get MATLAB to generate a floorplan and 3D plan of the building.
If I enter input into my code that I want 1 floor with a given number of rooms and dimensions the code works fine. If I enter more than 1 floor and then room dimensions the code fails with the error:
"Error using zeros
Size inputs must be scalar.
Error in Coursework_TW_testfile
roomLength=zeros(1,numberRooms);"
Here is my code:
clear;
numberFloors=input('Please input the number of floors required: ');
if numberFloors<=0
disp('Number of floors must be at least 1');
return
else
disp('Thanks');
end
floorLength=zeros(1,numberFloors);
floorWidth=zeros(1,numberFloors);
mainlooptotal=1;
while mainlooptotal<=numberFloors
for i=1:1:numberFloors
floorLength(i)=input(['Please enter floor ',num2str(mainlooptotal),' length: ']);
floorWidth(i)=input(['Please enter floor ',num2str(mainlooptotal),' width: ']);
floorHeight(i)=input(['Please enter floor ',num2str(mainlooptotal),' height: ']);
numberRooms(i)=input(['Please enter the number of rooms on floor ',num2str(mainlooptotal),': ']);
if floorLength(i)<=0 || floorWidth(i)<=0 || floorHeight(i)<=0 || numberRooms(i)<=0
disp('Floor dimensions and number of rooms must be greater than 0');
return
else
mainlooptotal=mainlooptotal+1;
end
roomLength=zeros(1,numberRooms);
roomWidth=zeros(1,numberRooms);
roomlooptotal=1;
while roomlooptotal<=numberRooms
for j=1:1:numberRooms
roomLength(j)=input(['Please enter room ',num2str(roomlooptotal),' length: ']);
roomWidth(j)=input(['Please enter room ',num2str(roomlooptotal),' width: ']);
roomHeight(j)=input(['Please enter room ',num2str(roomlooptotal),' height: ']);
if roomLength(j)<=0 || roomWidth(j)<=0 || roomHeight(j)<=0
disp('Room dimensions must be greater than 0');
return
else
roomlooptotal=roomlooptotal+1;
end
end
end
end
end
Thank you in advance.

Réponse acceptée

VBBV
VBBV le 12 Avr 2022
Modifié(e) : VBBV le 12 Avr 2022
roomLength=zeros(1,(numberRooms(i)));
roomWidth=zeros(1,(numberRooms(i)));
Give the size of numberRooms
  2 commentaires
VBBV
VBBV le 12 Avr 2022
Modifié(e) : VBBV le 12 Avr 2022
for j=1:1:(numberRooms(i))
Tobias Weymouth
Tobias Weymouth le 12 Avr 2022
Thanks that's working now :)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by