i should add that my dsired range for the x-axis is 0 to 12, but when i run the script it starts from -6 and goes to 12.
Why is xlim not working?
30 views (last 30 days)
Show older comments
Lorie Ann
on 6 Nov 2025 at 20:34
Commented: Matt J
on 7 Nov 2025 at 13:50
Hello! I am new to Matlab and am working on my first major assignment. I have a code that does almost everything I want, but my xlim is not properly setting my x axis limit. I initially had the xlim after the hold on function, but it did not format for the entire run. I now have it after the first plots and after every ginput() (as that was the fix i was recommended). It currently starts off with the incorrect x axis, and then formats to the range i want, but I would like it to fit within my range from beginning to end. What is the issue with my script that is causing this issue, and what can I do to fix it?
i attatched my script to this as a file. all help is appreciated!
Accepted Answer
Matt J
on 6 Nov 2025 at 21:26
Edited: Matt J
on 6 Nov 2025 at 21:34
Calling axis equal triggers an xlim,ylim adjustment.
% Set parameters for main circle
center = [3,6];
radius = 2;
angleStart = 0;
angleEnd = 2*pi;
circleAngle = linspace( angleStart, angleEnd); % Creates range of angle values
% Calculate x and y values
x = center(1) + radius * cos(circleAngle);
y = center(2) + radius * sin(circleAngle);
% PLot main circle
plot(x,y, "b", "LineWidth", 1.5); axis equal;
hold on;
plot(3,6, "bx", "LineWidth", 1.5); % Plot center x
xlim([0, 12]);
ylim([0, 12]);
% Prevents ellipses shape
grid on; % Applies grid to figure
% Prompt user for description location
disp(" "); % Added to improve readibility in command window
disp("Select location for circle description:");
[x1,y1] = ginput(1);
% Display text description
text(x1,y1, "center at (" + center(1) + "," + center(2) + ")");
% Prompt user for endpoint
disp(" ");
disp(" Select endpoint for line between description and circle:");
[x2, y2] = ginput(1);
% Draw line between description and circle
plot([x1, x2], [y1, y2], 'b-.');
% Repeat for second circle
center2 = [6,2];
radius2 = 0.90;
x2 = center2(1) + radius2 * cos(circleAngle);
y2 = center2(2) + radius2 * sin(circleAngle);
plot(x2, y2, "m", "LineWidth", 3);
plot(6, 2, "mx", "LineWidth", 1.5);
disp(" ");
disp("Select location for circle description:");
[x3,y3] = ginput(1);
text(x3,y3, "center at (" + center2(1) + "," + center2(2) + ")");
disp(" ");
disp(" Select endpoint for line between description and circle:");
[x4, y4] = ginput(1);
plot([x3, x4], [y3, y4], 'm-.');
2 Comments
Matt J
on 7 Nov 2025 at 13:50
You're welcome, but please Accept-click the answer to indicate that it resolved your question.
More Answers (0)
See Also
Categories
Find more on Graphics Object Programming 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!