How to have a plot3 vector and sphere on same figure?

조회 수: 15 (최근 30일)
Maxwell DeVoe
Maxwell DeVoe 2018년 7월 23일
댓글: Maxwell DeVoe 2018년 7월 25일
Hello all, I'm currently trying to write a program where I'm drawing a line within a sphere, and it changes size based on the dataset. The line will always start at the origin, but I want the line to dynamically change based on the endpoint given, which will be given by a dataset. The problem I'm running into is that I can't get this line to be on the same figure as the sphere, even with hold on. when it did work previously, it just traced a path and left it there, which obscured the motion of the vector throughout the space. I've posted the code below, and any help would be appreciated.
figure(1) %creating animation window
sphere % creating sphere for visualization
alpha('clear') % making sphere clear for visualization
hold on; % make it so that all animations are drawn on same figure
for k=1:1200 %looping through data set
v = z(k); % z coordinate
vec1 = [0,0,0];
vec2 = [0,0,v];
vec = [vec2;vec1];
figure(1);
h = plot3(vec(:,1),vec(:,2),vec(:,3), 'LineWidth', 2, 'Color', 'r');%updating the line animation data set
clf(h, 'reset');
refreshdata;
drawnow;
end

채택된 답변

Ryan Takatsuka
Ryan Takatsuka 2018년 7월 24일
Try something like this:
figure(1) %creating animation window
sphere % creating sphere for visualization
alpha('clear') % making sphere clear for visualization
hold on; % make it so that all animations are drawn on same figure
t = linspace(0,1,1200);
z = cos(4*pi*t);
for k=1:1200 %looping through data set
% Get each of the plotted components
children = get(gca, 'children');
% If there is only one, this is the first iteration, so do nothing. Otherwise, remove the line
if length(children)>1
delete(children(1));
end
v = z(k); % z coordinate
vec1 = [0,0,0];
vec2 = [0,0,v];
vec = [vec2;vec1];
figure(1);
h = plot3(vec(:,1),vec(:,2),vec(:,3), 'LineWidth', 2, 'Color', 'r');%updating the line animation data set
% This needs to be here
pause(.001)
end
  댓글 수: 2
Maxwell DeVoe
Maxwell DeVoe 2018년 7월 25일
Thank you so much! I understand what you have done, but I just had a question about z. Why does it have to be set to cos(4*pi)? Forgive me if it's obvious, my math is a bit rusty.
Ryan Takatsuka
Ryan Takatsuka 2018년 7월 25일
Oh I just randomly set that function to describe the z variable because you didn't include values for z in your initial code posted here.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Maxwell DeVoe
Maxwell DeVoe 2018년 7월 25일
Oh ok, so it doesn't use the values I have in my z data field?
  댓글 수: 2
Ryan Takatsuka
Ryan Takatsuka 2018년 7월 25일
No, but it should work with any vector of z values (You might have to change the indexing of the for-loop to loop over z depending on the length of the vector).
Maxwell DeVoe
Maxwell DeVoe 2018년 7월 25일
Sweet, it works with my data now. Thank you Ryan, you were a big help.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Animation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by