Why is xlim not working?

30 visualizaciones (últimos 30 días)
Lorie Ann
Lorie Ann el 6 de Nov. de 2025 a las 20:34
Comentada: Matt J el 7 de Nov. de 2025 a las 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 comentario
Lorie Ann
Lorie Ann el 6 de Nov. de 2025 a las 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.

Iniciar sesión para comentar.

Respuesta aceptada

Matt J
Matt J el 6 de Nov. de 2025 a las 21:26
Editada: Matt J el 6 de Nov. de 2025 a las 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 comentarios
Lorie Ann
Lorie Ann el 7 de Nov. de 2025 a las 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 el 7 de Nov. de 2025 a las 13:50
You're welcome, but please Accept-click the answer to indicate that it resolved your question.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Graphics Object Programming en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by