Why is my framework plot not working as intended?

3 views (last 30 days)
Trying to plot a framework using the following code:
x_punkter=[1 3 3 5 5 7 7 5 5 3 3 1 1 1 1 3 1 3 3 5 3 5 3 3 5 5]; y_punkter=[0 0 0 0 0 -2 -2 -2 -2 -2 -2 -2 -2 0 0 -2 -2 0 0 -2 -2 0 -2 0 -2 0];
for i=1:2:(length(x_punkter)/2) plot([x_punkter(i) x_punkter(i+1)],[y_punkter(i) y_punkter(i+1)],'o-') hold on end axis equal axis([0 8 -4 2])
It works like intended up to i=7 and then it stops plotting it seems. Anyone got an idea why and can you please help me in that case. Thanks in advance.

Answers (1)

Abhishek
Abhishek on 7 Mar 2025
Hi Hackle,
The issue that you are facing is likely due to the loop’s termination condition. In the provided code, you are using “length(x_punkter)/2” as the upper bound, which only iterates through half of the points. This is because “length(x_punkter)/2” evaluates to 13, but you have 26 points in total.
To fix this issue, you need to loop through all the 26 points. To do so, adjust the loop's termination condition to iterate through the entire set of points. This can be achieved by using “length(x_punkter)” as the upper bound in the loop condition.
I have made the above changes in the given code and tried it on MATLAB R2024b.
Here is the corrected code:
x_punkter = [1 3 3 5 5 7 7 5 5 3 3 1 1 1 1 3 1 3 3 5 3 5 3 3 5 5];
y_punkter = [0 0 0 0 0 -2 -2 -2 -2 -2 -2 -2 -2 0 0 -2 -2 0 0 -2 -2 0 -2 0 -2 0];
% Loop through all points
for i = 1:2:length(x_punkter)
plot([x_punkter(i) x_punkter(i+1)], [y_punkter(i) y_punkter(i+1)], 'o-');
hold on;
end
axis equal;
axis([0 8 -4 2]);
The above code will create the attached plot. Hope this solves the issue.

Community Treasure Hunt

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

Start Hunting!