Stored array won't work when variable is greater then one
Mostrar comentarios más antiguos
My stored array for coordinates does not work when the amount of coordinates on a level is greater then 1. The line of code that doesn't work is:
storedresidentialcoordinates(b,ca,:)=residentialcoordinates;
The full code:
numberoffloors=input('How many floors are there?');
if numberoffloors < 0
message1=msgbox('An invalid number has been entered');
return
end
for a=1:1:numberoffloors
spaces=input('How many spaces are on this floor?');
if spaces < 0
message2=msgbox('An invalid number has been entered');
return
end
numberofspaces(:,a)=spaces;
end
%INSERT MESSAGE BOX%
areares=input('What is the minimum area size for residential rooms?');
areaoff=input('What is the minimum area size for office rooms?');
areaedu=input('What is the minimum area size for education rooms?');
areatoi=input('What is the minimum area size for toilets?');
areastor=input('What is the minimum area size for storage rooms?');
if (areares || areaoff || areaedu || areatoi || areastor) < 0
messagea1=msgbox('An invalid number was entered');
return
end
%INSERT MESSAGE BOX%
ratioresoff=input('What is the ratio of residential to office rooms?');
ratioresedu=input('What is the ratio of residential to education rooms?');
ratiorestoi=input('What is the ratio of residential to toilets?');
ratioresstor=input('What is the ratio of residential to storage rooms?');
ratiooffedu=input('What is the ratio of office to education rooms?');
ratioofftoi=input('What is the ratio of office to toilets?');
ratiooffstor=input('What is the ratio of office to storage rooms?');
ratioedutoi=input('What is the ratio of education to toilet?');
ratioedustor=input('What is the ratio of education to storage rooms?');
ratiotoistor=input('What is the ratio of toilets to storage rooms?');
if (ratioresoff || ratioresedu || ratiorestoi || ratioresstor || ratiooffedu || ratioofftoi || ratiooffstor || ratioedutoi || ratioedustor || ratiotoistor) < 0
messagea2=msgbox('An invalid ratio has been entered');
return
end
for b=1:1:numberoffloors
residential=input('How many residential rooms are there?');
office=input('How many offices are there?');
education=input('How many educational rooms are there?');
toilet=input('How many toilets are there?');
storage=input('How many storage rooms are there?');
figure(b);
if (residential || office || education || toilet || storage) < 0
message3=msgbox('An invalid number has been entered');
return
end
if ratioresoff~=(residential/office) || ratioresedu~=(residential/education) || ratiorestoi~=(residential/toilet) || ratioresstor~=(residential/storage) || ratiooffedu~=(office/education) || ratioofftoi~=(office/toilet) || ratiooffstor~=(office/storage) || ratioedutoi~=(education/toilet) || ratioedustor~=(education/storage) || ratiotoistor~=(toilet/storage)
messagea3=msgbox('The number of rooms entered does not equal the ratios entered');
end
c=[residential office education toilet storage];
total=0;
total=(residential+office+education+toilet+storage);
if total ~= numberofspaces(1,b)
message4=msgbox('The number of spaces does not equal the total of all the individual rooms. Please retry');
return
elseif total == numberofspaces(1,b)
room(b,:)=c;
end
for ca=1:1:residential
xrescoord=input('What is the x-coordinate for this room?');
if xrescoord < 0
message5=msgbox('An invalid number has been entered');
end
yrescoord=input('What is the y-coordinate for this room?');
if yrescoord < 0
message6=msgbox('An invalid number has been entered');
return
end
reswidth=input('What is the width of this residential room?');
if reswidth < 1
message7=msgbox('An invalid number has been entered');
return
end
resheight=input('What is the height of this residential room?');
if resheight < 1
message8=msgbox('An invalid number has been entered');
return
end
if resheight*reswidth < areares
message25=msgbox('Area is less then minimum specification');
return
end
rectangle('position',[xrescoord yrescoord reswidth resheight]);
hold on;
xrcoord(:,ca)=xrescoord
yrcoord(:,ca)=yrescoord
residentialcoordinates=[xrcoord;yrcoord]
storedresidentialcoordinates(b,ca,:)=residentialcoordinates;
end
5 comentarios
Jan
el 3 de Mayo de 2018
"Does not work" is a weak description of the problem. Please post the error message.
"storedresidentialcoordinates" is an ugly name for a variable. It is not clear to me, what "amount of coordinates on a level is greater then 1" means.
Thomas Sargent
el 3 de Mayo de 2018
Jan
el 3 de Mayo de 2018
The error message means, that storedresidentialcoordinates(b,ca,:) and residentialcoordinates have different sizes. Then the assignment cannot work.
I will not run you code an fight with the ugly bunch of input commands. This is a very inconvenient method to provide values. One typo and you have to restart the code. And if the code runs, there is no chance to check, if there has been a typo during the input. Therefore the result of the code is hardly reproducible.
The debugging is hard also.
Thomas Sargent
el 3 de Mayo de 2018
Respuestas (1)
Jan
el 3 de Mayo de 2018
xrcoord(:,ca)=xrescoord
yrcoord(:,ca)=yrescoord
These lines let xrcoord and yrcoord grow in each iteration. Then
residentialcoordinates=[xrcoord;yrcoord]
Has a different size in each iteration also. In consequence
storedresidentialcoordinates(b,ca,:)=residentialcoordinates;
cannot work in a loop. Maybe you want:
storedresidentialcoordinates(b,ca,:) = [xrescoord; yrescoord];
instead.
Categorías
Más información sobre Commercial & Off-Highway Vehicles en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!