for loop keeps putting out error

조회 수: 1 (최근 30일)
Heather Palardy
Heather Palardy 2023년 11월 7일
편집: Matt J 2023년 11월 7일
% Define the dimensions of the garden area (in square feet)
garden_width = 6; % You may adjust this to fit your garden dimensions
garden_length = 150;
% Define the distance between each garden layout
spacing = 30;
% Create a single figure for the entire garden layout
figure;
axis([0, garden_width, 0, garden_length]);
axis equal;
hold on;
% Define the plant coordinates (x, y) and sizes for each layout
plants = {
'Beach Plum', [5, 20], 2, 'b';
'Hibiscus', [15, 20], 2, 'g';
'NE Aster', [25, 20], 2, 'r';
'Iris Versicolor', [10, 10], 1, 'c';
'Bee Balm', [20, 10], 1, 'm';
'Rosa Palustris', [5, 5], 1.5, 'y';
'Viburnum Dentatum', [15, 5], 1.5, 'k';
'Little Blue Stem', [25, 5], 1, 'w';
};
% Loop to generate garden layouts along the length
for start_y = 0:spacing:garden_length
% Plot the garden layout for the current section
for i = 1:size(plants, 1)
plant_name = plants{i, 1};
coordinates = plants{i, 2};
size = plants{i, 3};
color = plants{i, 4};
% Plot a filled circle to represent each plant with the specified color
x = coordinates(1);
y = coordinates(2);
r = size;
theta = linspace(0, 2*pi, 100);
x_circle = r * cos(theta) + x;
y_circle = r * sin(theta) + y + start_y;
% Plot the filled circle for the plant
plot(x_circle, y_circle, 'DisplayName', plant_name, 'Color', color, 'MarkerFaceColor', color);
end
end
Unable to use a value of type cell as an index.

size appears to be both a function and a variable. If this is unintentional, use 'clear size' to remove the variable 'size' from the workspace.
% Set labels for the plants
title('Garden Layout');
xlabel('Width (feet)');
ylabel('Length (feet)');
% Customize the appearance as needed
legend('Location', 'BestOutside');
hold off;

답변 (3개)

Matt J
Matt J 2023년 11월 7일
편집: Matt J 2023년 11월 7일
% Loop to generate garden layouts along the length
for start_y = 0:spacing:garden_length
% Plot the garden layout for the current section
for i = 1:size(plants, 1)
plant_name = plants{i, 1};
coordinates = plants{i, 2};
plant_size = plants{i, 3};
plant_color = plants{i, 4};
% Plot a filled circle to represent each plant with the specified color
x = coordinates(1);
y = coordinates(2);
r = plant_size;
theta = linspace(0, 2*pi, 100);
x_circle = r * cos(theta) + x;
y_circle = r * sin(theta) + y + start_y;
% Plot the filled circle for the plant
plot(x_circle, y_circle, 'DisplayName', plant_name, 'Color', plant_color, 'MarkerFaceColor', plant_color);
end
end
% Set labels for the plants
title('Garden Layout');
xlabel('Width (feet)');
ylabel('Length (feet)');
% Customize the appearance as needed
legend('Location', 'BestOutside');
hold off;

the cyclist
the cyclist 2023년 11월 7일
Don't use the keyword size as a variable name. I changed it in the code below:
% Define the dimensions of the garden area (in square feet)
garden_width = 6; % You may adjust this to fit your garden dimensions
garden_length = 150;
% Define the distance between each garden layout
spacing = 30;
% Create a single figure for the entire garden layout
figure;
axis([0, garden_width, 0, garden_length]);
axis equal;
hold on;
% Define the plant coordinates (x, y) and sizes for each layout
plants = {
'Beach Plum', [5, 20], 2, 'b';
'Hibiscus', [15, 20], 2, 'g';
'NE Aster', [25, 20], 2, 'r';
'Iris Versicolor', [10, 10], 1, 'c';
'Bee Balm', [20, 10], 1, 'm';
'Rosa Palustris', [5, 5], 1.5, 'y';
'Viburnum Dentatum', [15, 5], 1.5, 'k';
'Little Blue Stem', [25, 5], 1, 'w';
};
% Loop to generate garden layouts along the length
for start_y = 0:spacing:garden_length
% Plot the garden layout for the current section
for i = 1:size(plants, 1)
plant_name = plants{i, 1};
coordinates = plants{i, 2};
sizevar = plants{i, 3};
color = plants{i, 4};
% Plot a filled circle to represent each plant with the specified color
x = coordinates(1);
y = coordinates(2);
r = sizevar;
theta = linspace(0, 2*pi, 100);
x_circle = r * cos(theta) + x;
y_circle = r * sin(theta) + y + start_y;
% Plot the filled circle for the plant
plot(x_circle, y_circle, 'DisplayName', plant_name, 'Color', color, 'MarkerFaceColor', color);
end
end
% Set labels for the plants
title('Garden Layout');
xlabel('Width (feet)');
ylabel('Length (feet)');
% Customize the appearance as needed
legend('Location', 'BestOutside');
hold off;

Voss
Voss 2023년 11월 7일
You have defined a variable called size, which means that the next time the code tries to use the built-in size function (i.e., in for i = 1:size(plants,1) on the second iteration of the outer loop), it doesn't use the built-in size function but instead tries to index your size variable, and that produces the error.
Change your variable size to something else; here I've used siz.
% Define the dimensions of the garden area (in square feet)
garden_width = 6; % You may adjust this to fit your garden dimensions
garden_length = 150;
% Define the distance between each garden layout
spacing = 30;
% Create a single figure for the entire garden layout
figure;
axis([0, garden_width, 0, garden_length]);
axis equal;
hold on;
% Define the plant coordinates (x, y) and sizes for each layout
plants = {
'Beach Plum', [5, 20], 2, 'b';
'Hibiscus', [15, 20], 2, 'g';
'NE Aster', [25, 20], 2, 'r';
'Iris Versicolor', [10, 10], 1, 'c';
'Bee Balm', [20, 10], 1, 'm';
'Rosa Palustris', [5, 5], 1.5, 'y';
'Viburnum Dentatum', [15, 5], 1.5, 'k';
'Little Blue Stem', [25, 5], 1, 'w';
};
% Loop to generate garden layouts along the length
for start_y = 0:spacing:garden_length
% Plot the garden layout for the current section
for i = 1:size(plants, 1)
plant_name = plants{i, 1};
coordinates = plants{i, 2};
siz = plants{i, 3};
color = plants{i, 4};
% Plot a filled circle to represent each plant with the specified color
x = coordinates(1);
y = coordinates(2);
r = siz;
theta = linspace(0, 2*pi, 100);
x_circle = r * cos(theta) + x;
y_circle = r * sin(theta) + y + start_y;
% Plot the filled circle for the plant
plot(x_circle, y_circle, 'DisplayName', plant_name, 'Color', color, 'MarkerFaceColor', color);
end
end
% Set labels for the plants
title('Garden Layout');
xlabel('Width (feet)');
ylabel('Length (feet)');
% Customize the appearance as needed
legend('Location', 'BestOutside');
hold off;

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by