How to set up and 'if' function depending on the user inputs

3 visualizaciones (últimos 30 días)
Daniel Michell
Daniel Michell el 4 de Mayo de 2021
Editada: Scott MacKenzie el 4 de Mayo de 2021
Hi there,
I am creating some code to plot the coordinates of floor plans of floors a building. So far I have come up with this...
floorNumbers=input('Please enter the number of floors in the building: ');
for j=1:1:floorNumbers
floorSpaces(j)=input('Please enter the number of spaces of each floor: ');
for i=1:1:floorSpaces(j)
x(j,i)=input('Please enter the x coordinate of the space: ');
y(j,i)=input('Please enter the y coordinate of the space: ');
end
end
floorDrawing =input('Please enter the floor you wish to draw: ');
if floorDrawing...................
end
My question is how do I write the code to allow the drawing of an individual floor plan, without knowing how many floor plans there is going to be until the user inputs it. My plan is that when the user, for example inputs floor '2' after being ask which floor plan they wish to draw, I would plot the 2nd row of the x and y arrays. But without knowing how many floors there will be in advance, how would I go about this?
Many thanks in advance,
Dan
  1 comentario
Scott MacKenzie
Scott MacKenzie el 4 de Mayo de 2021
Editada: Scott MacKenzie el 4 de Mayo de 2021
Some clarification might help. Is your main concern ensuring that the floorDrawing number entered by the user actually exists? And, if not, reprompt the user? If that's the case, here's one approach:
while true
floorDrawing = input('Please enter the floor you wish to draw: ');
if floorDrawing <= length(floorSpaces)
break; % exit the loop if the user input is a valid floor number
end
end
% valid input, proceed to draw

Iniciar sesión para comentar.

Respuestas (1)

Walter Roberson
Walter Roberson el 4 de Mayo de 2021
floorNumbers=input('Please enter the number of floors in the building: ');
floorSpaces = zeros(1,floorNumbers);
x = zeros(floorNumbers, 0);
y = zeros(floorNumbers, 0);
for j=1:1:floorNumbers
floorSpaces(j)=input('Please enter the number of spaces of each floor: ');
if floorSpaces(j) > size(x,2)
x(:,end+1:floorSpaces(j)) = NaN;
y(:,end+1:floorSpaces(j)) = NaN;
end
x(j,:) = nan;
y(j,:) = nan;
for i=1:1:floorSpaces(j)
x(j,i)=input('Please enter the x coordinate of the space: ');
y(j,i)=input('Please enter the y coordinate of the space: ');
end
end
Now x and y will be 2D arrays, in which each row represents a floor. The number of defined floors is stored in floorNumbers. Each row of x and y will be nan-padded if the number of spaces for the floor is less than the maximum number of spaces for any floor.
So, given a floor number, you can access x(theFloor,:) and y(theFloor,:) and draw only as far as there are non-nan.
fx = x(theFloor,:);
fy = y(theFloor,:);
mask = isnan(fx) | isnan(fy);
fx(mask) = [];
fy(mask) = [];
Now fx and fy are the coordinates for this floor, with all nan removed.

Categorías

Más información sobre Parallel Computing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by