I have this code that is set up to animate a circle plot based on the circle's diameter. I am struggling with how to plot the circle based on the diameter in the for loop. I have one column vector of 1,667 columns. In each of those columns is the diameter (i.e. 1,667 diameters from data). I want to animate the circle changing based on those diameters. So, I need the plot in the for loop to go through, iterate through the columns, and update the plot, and but show that as an animation. I've attached my code. Please help with the plot portion of the code, thanks!

5 Comments

Kelly - your code is simply
animationWriter = VideoWriter('HSPtoHSPCircle');
open(animationWriter);
[a,b] = size(B);
for i = 1:a
for j = 1:b
% create data for circle plot or any other
% processing you want to do
f = ....; % plot your circle here using plot() or any other graphics primitive
frame = getframe(f);
videoWriter(animationWriter, frame);
end
end
close(animationWriter);
Is B your 1667x1 array of diameters? If so, why do you loop over the rows and columns of B? Or is B a matrix?
I would start with https://www.mathworks.com/matlabcentral/answers/98665-how-do-i-plot-a-circle-with-a-given-radius-and-center which describes how to draw a circle given its (x,y) coordinates and radius.
The code you are using is that which was given to you at https://www.mathworks.com/matlabcentral/answers/397442-animating-a-circle-based-on-diameter-data. Why haven't you tried to do something with this code instead of posing a new question?
Kelly McGuire
Kelly McGuire on 27 Apr 2018
Edited: Kelly McGuire on 27 Apr 2018
No, B is a 1x1667 array of diameters. I don't necessarily need to loop over all the rows since there is only one row, just need to loop over the columns. So, I think I could just do: for i = 1:length(B), but the size of i shows 1 in the value column in the workspace window. I am trying that code, but as a novice, I've never plotted a circle, let alone a plot that updates through a loop and then is output as an animation. I am hoping someone could show me how write code for a plot in a for loop that takes the i = 1:length(B) and gets used in the circle plot.
Ok, I tried the code in that link for a circle, and that code works. Now, the trouble I'm having is with my matrix dimensions. I'm not too good with the iteration coding yet, but here is my updated code. I'm getting the error: Matrix dimensions must agree Error in circle lin 12 Error in AnimatedCirclePlot line 11. I've attached both .m files.
Looks like I forgot all of the (i,j) in the xunit and yunit functions. That fixed the matrix dimensions problem. Now, a new error, one that really don't know how to solve. Index in position 2 exceeds array bounds (must not exceed 1). I know this is talking about the cos(th(i,j)) and sin(th(i,j)) in circle.m, but I'm not sure what the problem is really saying...

Sign in to comment.

 Accepted Answer

Ameer Hamza
Ameer Hamza on 27 Apr 2018
Edited: Ameer Hamza on 27 Apr 2018
@Kelly, As I answered on your previous question, that you need to fill the plot for circle yourself. If you are still not able to make that work then here is the complete code.
B = 10:10:1000; % test B vector. You will use your own B
animationWriter = VideoWriter('HSPtoHSPCircle');
open(animationWriter);
l = line();
l.XData = [];
l.YData = [];
xlim([-max(B) max(B)]);
ylim([-max(B) max(B)]);
theta = 0:pi/50:2*pi;
for i = 1:length(B)
% create data for circle plot or any other
% processing you want to do
xData = B(i)*cos(theta);
yData = B(i)*sin(theta);
l.XData = xData;
l.YData = yData;
frame = getframe(gcf);
writeVideo(animationWriter, frame);
end
close(animationWriter);
I created this animation using given code.

6 Comments

Thanks, there's a lot there I didn't know about. Still learning all of these commands and how to use them correctly.
That worked the way I wanted, thanks for the help.
You are welcome.
Kelly McGuire
Kelly McGuire on 30 Apr 2018
Edited: Kelly McGuire on 30 Apr 2018
Hey Ameer, wasn't sure how to direct this question to you in the Matlab Answers section, but it is related to the previous question and your code worked perfectly. Another plot I am trying to make is the same idea as the animated circle changing by diameter, but this time a line plot changing based on the number of water molecules at a given amino acid inside an ion channel. I am trying to modify the code you gave me to do this. Here is what I have so far, but I'm not sure what to put in the xlim or xData, because the x-data is just the name of the residue, so the xdata doesn't change, but a line needs a y-coordinate and an x-coordinate. An example of the plot and the matrix is attached, which is a plot from just the first row of # of water molecules in the 1667 x 4 matrix, as well as the modified code so far. At each of those 4 residues, the # number of waters is changing each frame, but I didn't want to plot just moving points, but an animated line of the # of waters changing. Maybe this same code can't be used for this though.
As far as changing the labels on X-axis is concerned, the following code can do it,
ax.XLim = [1 4]
ax.XTick = 1:4
ax.XTickLabel = {'residue 1', 'residue 2', 'residue 3', 'residue 4'}
As far as making an animation with moving line is concerned. It is quite different to this question. Here is an outline of how can you do it.
  • Make a linear interpolation of edges between two rows of your data matrix.
  • Instead of just drawing the rows of your matrix, also plot all the interpolated points. This way, the animation will look smooth.
If you still face a problem, you might want to start a new question, because the new problem is quite out of the scope for this question.
Sounds good, I'll post a new question. Was hoping the same code you gave me for the circle plot could do this.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!