Hi
i wnat to clear only data in fig
I try to cla reset but this command all initializes another settings.
i wnat remain other setting (ex. xlim, view etc...)
Eventually , I want animate rotating circle Thanks for your review
%%
clear all;
clc;
close all;
%%
% p_circle0 = circle(1,1,1);
n_circle_p = 50;
theta_c = 0:2*pi/(n_circle_p-1):2*pi;
p_circle = zeros(n_circle_p, 3);
r = 1;
for i = 1:n_circle_p
p_circle(i,:) = [r*cos(theta_c(i)), r*sin(theta_c(i)), 0];
end
% p_circle = [p_circle0, zeros(size(p_circle0,1),1)];
N = size(p_circle,1);
N_sample = 10;
theta = 0:pi/(N_sample-1):pi;
fig1 = figure(1);
p_circle2 = zeros(size(p_circle));
for i = 1:N
p_circle2(i,:) = (rotx(theta(1))*p_circle(i,:)')';
end
line(p_circle2(:,1),p_circle2(:,2),p_circle2(:,3));
grid on;
xlim([-1.5, 1.5]);
ylim([-1.5, 1.5]);
zlim([-1.5, 1.5]);
xlabel('x');
ylabel('y');
zlabel('z');
view([30, 40]);
%%
for j = 1:N_sample
p_circle2 = zeros(size(p_circle));
for i = 1:N
p_circle2(i,:) = (rotx(theta(j))*p_circle(i,:)')';
end
line(p_circle2(:,1),p_circle2(:,2),p_circle2(:,3));
line(p_circle2(1:2,1),p_circle2(1:2,2),p_circle2(1:2,3), 'Color', 'r');
grid on;
xlim([-1.5, 1.5]);
ylim([-1.5, 1.5]);
zlim([-1.5, 1.5]);
xlabel('x');
ylabel('y');
zlabel('z');
view([30, 40]);
pause(0.5);
cla reset;
drawnow % display updates
end

 Réponse acceptée

Voss
Voss le 28 Fév 2022
Modifié(e) : Voss le 28 Fév 2022

0 votes

Seems like your best bet in this situation would be to create those lines before your for loop and then set their XData, YData, ZData inside the loop. Something like this:
my_lines = [line() line('Color','r')];
for j = 1:N_sample
% calculate p_circle2
set(my_lines(1), ...
'XData',p_circle2(:,1), ...
'YData',p_circle2(:,2), ...
'ZData',p_circle2(:,3));
% similarly for my_lines(2)
end
instead of doing cla reset or similar.

5 commentaires

I don't understand what your code means
Couldn't apply but thank you so much
Now I have roughly achieved my goal
The problem was the location of the cla reset
I still haven't found a better way than cla reset
If there is a way to delete only data other than cla reset
I think it would be really convenient.
clear;
clc;
close all;
%Make Circle
center=[0 1];
n_circle_p = 40;
theta_c = 0:2*pi/(n_circle_p-1):2*pi;
%First Circle
p_circle = zeros(n_circle_p, 3);
r = 1;
for i = 1:n_circle_p
p_circle(i,:) = [r*cos(theta_c(i))+center(1), r*sin(theta_c(i))+center(2), 0];
end
%First Circle
s_circle = zeros(n_circle_p, 3);
r = 0.8;
for i = 1:n_circle_p
s_circle(i,:) = [r*cos(theta_c(i))+center(1), r*sin(theta_c(i))+center(2), 0];
end
%Rotate resolution(deg 0 to deg PI)
N = size(p_circle,1);
N_sample = 20;
theta = 0:2*pi/(N_sample-1):2*pi;
%Rotate resolution(deg 0 to deg PI)
cLINE_L = 40;
cLINE = zeros(cLINE_L, 3);
cLINE(:,1) = transpose(linspace(1.5,-1.5,40));
%Draw Setup
redstart = 1;
redend = 20;
kstart = 21;
kend = 40;
lim_min = -5;
lim_max = 5;
while true
for j = 1:N_sample
p_circle2 = zeros(size(p_circle));
s_circle2 = zeros(size(s_circle));
for i = 1:N
p_circle2(i,:) = (rotx(theta(j))*p_circle(i,:)')';
s_circle2(i,:) = (rotx(theta(j))*s_circle(i,:)')';
end
line(p_circle2(:,1),p_circle2(:,2),p_circle2(:,3));
line(s_circle2(:,1),s_circle2(:,2),s_circle2(:,3));
line(p_circle2(redstart:redend,1),p_circle2(redstart:redend,2),p_circle2(redstart:redend,3), 'Color', 'r');
line(p_circle2(kstart:kend,1),p_circle2(kstart:kend,2),p_circle2(kstart:kend,3), 'Color', 'k');
line(cLINE(:,1),cLINE(:,2),cLINE(:,3))
grid on;
xlim([lim_min, lim_max]);
ylim([lim_min, lim_max]);
zlim([lim_min, lim_max]);
xlabel('x');
ylabel('y');
zlabel('z');
view([45, 45]);
drawnow % display updates
pause(0.1);
% if j ~= N_sample
cla reset;
% end
end
end
Voss
Voss le 28 Fév 2022
Oh yeah, that's true, you should do drawnow() before cla reset.
Voss
Voss le 28 Fév 2022
As I understand the problem, it was that cla or cla reset changes axes settings that you don't want changed, and you asked whether there is a way to just clear the plot (i.e., delete the lines) but keep the axes settings the same.
Yes, you can avoid changing any axes settings at all by operating on the lines in the axes directly: specifically, by storing the lines' handles returned from the line() function and calling delete() on them when you want to delete those lines. But the better way is to create the lines one time and update them each time through your for loop, rather than deleting them and recreating them each time through.
Here's how this approach might look applied to the while true/for loop in the second half of the code from your most recent comment:
clear;
clc;
close all;
%Make Circle
center=[0 1];
n_circle_p = 40;
theta_c = 0:2*pi/(n_circle_p-1):2*pi;
%First Circle
p_circle = zeros(n_circle_p, 3);
r = 1;
for i = 1:n_circle_p
p_circle(i,:) = [r*cos(theta_c(i))+center(1), r*sin(theta_c(i))+center(2), 0];
end
%First Circle
s_circle = zeros(n_circle_p, 3);
r = 0.8;
for i = 1:n_circle_p
s_circle(i,:) = [r*cos(theta_c(i))+center(1), r*sin(theta_c(i))+center(2), 0];
end
%Rotate resolution(deg 0 to deg PI)
N = size(p_circle,1);
N_sample = 20;
theta = 0:2*pi/(N_sample-1):2*pi;
%Rotate resolution(deg 0 to deg PI)
cLINE_L = 40;
cLINE = zeros(cLINE_L, 3);
cLINE(:,1) = transpose(linspace(1.5,-1.5,40));
%Draw Setup
redstart = 1;
redend = 20;
kstart = 21;
kend = 40;
lim_min = -5;
lim_max = 5;
% set the axes settings first:
grid on;
xlim([lim_min, lim_max]);
ylim([lim_min, lim_max]);
zlim([lim_min, lim_max]);
xlabel('x');
ylabel('y');
zlabel('z');
view([45, 45]);
% create the lines you need now.
% only need to specify their color, no data yet (except for the last one
% (cLINE), whose data does not change inside the for loop)
my_lines = [ ...
line() ...
line() ...
line('Color','r') ...
line('Color','k') ...
line( ...
'XData',cLINE(:,1), ...
'YData',cLINE(:,2), ...
'ZData',cLINE(:,3)) ...
];
while true
for j = 1:N_sample
p_circle2 = zeros(size(p_circle));
s_circle2 = zeros(size(s_circle));
for i = 1:N
p_circle2(i,:) = (rotx(theta(j))*p_circle(i,:)')';
s_circle2(i,:) = (rotx(theta(j))*s_circle(i,:)')';
end
% now set the lines' data. that is, update the existing
% lines from the new p_circle2 and s_circle2
set(my_lines(1), ...
'XData',p_circle2(:,1), ...
'YData',p_circle2(:,2), ...
'ZData',p_circle2(:,3));
set(my_lines(2), ...
'XData',s_circle2(:,1), ...
'YData',s_circle2(:,2), ...
'ZData',s_circle2(:,3));
set(my_lines(3), ...
'XData',p_circle2(redstart:redend,1), ...
'YData',p_circle2(redstart:redend,2), ...
'ZData',p_circle2(redstart:redend,3));
set(my_lines(4), ...
'XData',p_circle2(kstart:kend,1), ...
'YData',p_circle2(kstart:kend,2), ...
'ZData',p_circle2(kstart:kend,3));
drawnow % display updates
pause(0.1);
end
end
Wow, that's right.
'delete()' is the way I think it is.
But I think the method you suggested is nicer.
This is where I first learned how to use 'set'.
thank you so much.
In addition, to move more smoothly
Starting with J at 2, it was perfect.
while true
for j = 2:N_sample
p_circle2 = zeros(size(p_circle));
s_circle2 = zeros(size(s_circle));
for i = 1:N
p_circle2(i,:) = (rotx(theta(j))*p_circle(i,:)')';
s_circle2(i,:) = (rotx(theta(j))*s_circle(i,:)')';
end
% now set the lines' data. that is, update the existing
% lines from the new p_circle2 and s_circle2
set(my_lines(1), ...
'XData',p_circle2(:,1), ...
'YData',p_circle2(:,2), ...
'ZData',p_circle2(:,3));
set(my_lines(2), ...
'XData',s_circle2(:,1), ...
'YData',s_circle2(:,2), ...
'ZData',s_circle2(:,3));
set(my_lines(3), ...
'XData',p_circle2(redstart:redend,1), ...
'YData',p_circle2(redstart:redend,2), ...
'ZData',p_circle2(redstart:redend,3));
set(my_lines(4), ...
'XData',p_circle2(kstart:kend,1), ...
'YData',p_circle2(kstart:kend,2), ...
'ZData',p_circle2(kstart:kend,3));
drawnow % display updates
pause(0.1);
end
Voss
Voss le 28 Fév 2022
I'm glad it's working!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Discrete Data Plots dans Centre d'aide 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