Why is xlim not working?

29 vues (au cours des 30 derniers jours)
Lorie Ann
Lorie Ann le 6 Nov 2025 à 20:34
Commenté : Matt J le 7 Nov 2025 à 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!
  1 commentaire
Lorie Ann
Lorie Ann le 6 Nov 2025 à 20:35
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.

Connectez-vous pour commenter.

Réponse acceptée

Matt J
Matt J le 6 Nov 2025 à 21:26
Modifié(e) : Matt J le 6 Nov 2025 à 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 commentaires
Lorie Ann
Lorie Ann le 7 Nov 2025 à 3:32
This is so good to know! I could not remove axis equal; entirely because doing so warped my circles, but I moved it to come before xlim and ylim and it fixed the issue! Thank you so much!
Matt J
Matt J le 7 Nov 2025 à 13:50
You're welcome, but please Accept-click the answer to indicate that it resolved your question.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Graphics Object Programming dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by