Zeros error when entering matrix data

2 ビュー (過去 30 日間)
Tobias Weymouth
Tobias Weymouth 2022 年 4 月 12 日
コメント済み: Tobias Weymouth 2022 年 4 月 12 日
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.

採用された回答

VBBV
VBBV 2022 年 4 月 12 日
編集済み: VBBV 2022 年 4 月 12 日
roomLength=zeros(1,(numberRooms(i)));
roomWidth=zeros(1,(numberRooms(i)));
Give the size of numberRooms
  2 件のコメント
VBBV
VBBV 2022 年 4 月 12 日
編集済み: VBBV 2022 年 4 月 12 日
for j=1:1:(numberRooms(i))
Tobias Weymouth
Tobias Weymouth 2022 年 4 月 12 日
Thanks that's working now :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by