Zeros error when entering matrix data
1 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!