Zeros error when entering matrix data
조회 수: 4 (최근 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에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!