for loop keeps putting out error

1 view (last 30 days)
Heather Palardy
Heather Palardy on 7 Nov 2023
Edited: Matt J on 7 Nov 2023
% 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;

Answers (3)

Matt J
Matt J on 7 Nov 2023
Edited: Matt J on 7 Nov 2023
% 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 on 7 Nov 2023
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 on 7 Nov 2023
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;

Categories

Find more on Line Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!