Animation of PATCH-Figure

7 visualizzazioni (ultimi 30 giorni)
Kovacs Mario
Kovacs Mario il 1 Mar 2024
Commentato: Kovacs Mario il 1 Mar 2024
Hi,
I tried to create an animation with PATCH but unfortunately, it only shows the final result and does not animate the whole process.
Just the values of the colorbar change.
(Picture at the bottom).
Do you have any idea that how can I create a color-changing process?
My code can be read below:
STEPS = 40;
q = zeros(14,STEPS);
q(:,1) = [0;0;0;0;0;0;0;0;0;0;0;0;0;0];
q(:,2) = [0;0;0.01;0.001;0.01;0.001;0;0;0.0025;0.00025;0.0025;0.00025;0.005;0];
for i = 3 : STEPS
q(:,i) = q(:,i-1) * 1.05;
end%for
RGB = [0 0 1
0 0.5 1
0 1 1
0 1 0.5
0 1 0
0.5 1 0
1 1 0
1 0.5 0
1 0 0];
Q = q(:,STEPS);
Q = Q(1:2:end);
% data for patch
msh_1.nn_3sz = [3,2,7;2,6,7;5,3,7;6,5,7];
msh_1.nn_4sz = [5,4,1,6];
msh_1.xy = [0,0;2,0;2,1;0,1;1,1;1,0;1.5,0.5];
figure (8)
qxy="qx";
for i = 1:STEPS
Q = q(:,i);
QX = Q(1:2:size(Q,1)-1);
QY = Q(2:2:size(Q,1));
switch qxy % displacements in X and Y dimensions
case "qx"
QQ = QX;
case "qy"
QQ = QY;
case "q"
QQ = sqrt(QX.^2+QY.^2);
end%switch
set(gcf, 'color', 'w')
colormap(RGB);
colorbar('EastOutside')
patch('Faces',msh_1.nn_3sz,'Vertices', msh_1.xy,'FaceVertexCData',QQ,'FaceColor','interp')
patch('Faces',msh_1.nn_4sz,'Vertices', msh_1.xy,'FaceVertexCData',QQ,'FaceColor','interp')
axis equal
drawnow()
end%for
The PATCH figure:
Do you have any idea how can I solve it?
Thank you so much for your helpful comments!

Risposta accettata

Benjamin Kraus
Benjamin Kraus il 1 Mar 2024
Modificato: Benjamin Kraus il 1 Mar 2024
There are two issues with your code:
  1. You are calling patch repeatedly, which is adding a new patch in each loop, without removing the old patches, so the patches are overlapping one another. This isn't really causing an issue, but it is not very efficient, and could easily cause issues in the future or with different animations.
  2. You are not fixing the color limits, which means that each time the loop runs it is picking color limits to tightly crop the data. This is the real reason you only see the colorbar changing on each loop.
The quickest fix to your code is to call clim (or previously called caxis) before your loop, or within your loop, to fix the color imits at a single value.
clim([0 0.0639])
But, you really want to be more efficient with your patches:
  • Either delete the old patches each loop before creating new ones.
  • But even better is reusing the patches each loop.
For example:
STEPS = 40;
q = zeros(14,STEPS);
q(:,1) = [0;0;0;0;0;0;0;0;0;0;0;0;0;0];
q(:,2) = [0;0;0.01;0.001;0.01;0.001;0;0;0.0025;0.00025;0.0025;0.00025;0.005;0];
for i = 3 : STEPS
q(:,i) = q(:,i-1) * 1.05;
end%for
RGB = [0 0 1
0 0.5 1
0 1 1
0 1 0.5
0 1 0
0.5 1 0
1 1 0
1 0.5 0
1 0 0];
Q = q(:,STEPS);
Q = Q(1:2:end);
% data for patch
msh_1.nn_3sz = [3,2,7;2,6,7;5,3,7;6,5,7];
msh_1.nn_4sz = [5,4,1,6];
msh_1.xy = [0,0;2,0;2,1;0,1;1,1;1,0;1.5,0.5];
figure (8)
qxy="qx";
% Create two patches that can be reused.
p1 = patch();
p2 = patch();
% Do one-time configuration of the axes and figure
set(gcf, 'color', 'w')
colormap(RGB);
colorbar('EastOutside')
axis equal
% Fix the color limits
clim([0 0.0639])
for i = 1:STEPS
Q = q(:,i);
QX = Q(1:2:size(Q,1)-1);
QY = Q(2:2:size(Q,1));
switch qxy % displacements in X and Y dimensions
case "qx"
QQ = QX;
case "qy"
QQ = QY;
case "q"
QQ = sqrt(QX.^2+QY.^2);
end%switch
% Reuse/update the existing patches with the new data:
set(p1, 'Faces',msh_1.nn_3sz,'Vertices', msh_1.xy,'FaceVertexCData',QQ,'FaceColor','interp')
set(p2, 'Faces',msh_1.nn_4sz,'Vertices', msh_1.xy,'FaceVertexCData',QQ,'FaceColor','interp')
drawnow()
end %for
  1 Commento
Kovacs Mario
Kovacs Mario il 1 Mar 2024
Thank you so much for your answer!
It was very helpful and clear!

Accedi per commentare.

Più risposte (1)

Magnus
Magnus il 1 Mar 2024
Modificato: Magnus il 1 Mar 2024
Sorry did not see the much better answer above before i answerd
Right now you only view the second call of patch (msh_1.nn_4sz) in each loop as it overwrites msh_1.nn_3sz. To view both in the same figure use hold on and hold off. Then you get an animation where new colours is added. Is that what you wanted to animate?
Other tips:
If you write in live script and run the code you can save your animation as a GIF in the figure options (hover over the figure)
set(gcf, 'color', 'w')
colormap(RGB);
colorbar('EastOutside')
patch('Faces',msh_1.nn_3sz,'Vertices', msh_1.xy,'FaceVertexCData',QQ,'FaceColor','interp')
hold on % hold the first patch
patch('Faces',msh_1.nn_4sz,'Vertices', msh_1.xy,'FaceVertexCData',QQ,'FaceColor','interp')
axis equal
drawnow
hold off % turn off before the next iteration
  1 Commento
Kovacs Mario
Kovacs Mario il 1 Mar 2024
Thank you so much for your answer! :)
It was helpful too!

Accedi per commentare.

Categorie

Scopri di più su Animation in Help Center e File Exchange

Prodotti


Release

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by