Drawing and animation in Matlab
20 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello, I'm sorry for my English, but I hope you will understand me right)
I want to visualize data in Matlab - plot some lines, cycles and other objects according to the matrix. For example:
I have 3 different states, shown in circles, and matrix, which contains the path of these states (See Figure) http://dl.dropbox.com/u/2827213/Stuff/Pics/Scheme_Plot.png How can I plot cycles, pathes and animate it- refresh every time with matrix changing.
I'm sure that there are instruments in Matlab for my purposes, but I can find it. Can you help me?
1 commentaire
Réponse acceptée
Paulo Silva
le 25 Fév 2011
I hope I'm not doing your homework
clf
a=[1:3;1:3;1:3;1:3;1:3];
plot(a,'bo','MarkerSize',25)
axis([0 6 0 4]);
%c=[3 3 2 1 1]
c=[1 2 1 3 2]
%c=[3 3 3 2 3]
for b=1:4
line([b b+1],[4-c(b) 4-c(b+1)],'Color',[1 0 0])
pause(1);
end
Plus de réponses (4)
Paulo Silva
le 25 Fév 2011
function drawpj
fig=figure;
init
set(fig,'CloseRequestFcn',@my_closefcn)
butt=uicontrol('Style','pushbutton','String','Start',...
'Callback',@drawlines,...
'Units','Normalized','Position',[0.5 0.1 0.1 0.1],...
'Parent',fig);
butt1=uicontrol('Style','pushbutton','String','Clean',...
'Callback',@clean,...
'Units','Normalized','Position',[0.6 0.1 0.1 0.1],...
'Parent',fig);
butt2=uicontrol('Style','pushbutton','String','Demo',...
'Callback',@dem,...
'Units','Normalized','Position',[0.7 0.1 0.1 0.1],...
'Parent',fig);
ed=uicontrol('Style','edit','String','','FontSize',12,...
'Callback',@drawlines,...
'Units','Normalized','Position',[0.4 0.1 0.1 0.1],...
'Parent',fig);
tocheck=uicontrol('Style','checkbox','String','loop','FontSize',12,...
'Units','Normalized','Position',[0.8 0.1 0.1 0.1],...
'Parent',fig);
function my_closefcn(gg,gg1)
set(tocheck,'Value',get(tocheck,'Min'))
delete(fig)
end
function clean(gg,gg1)
set(ed,'String','')
init
end
function dem(gg,gg1)
a=[randperm(3) randperm(3)];
b=num2str(a(1:5));
b=strrep(b,' ','');
set(ed,'String',b)
drawlines
if (~ishandle(fig)),return,end
if get(tocheck,'Value')==get(tocheck,'Max')
pause(0.5)
dem
end
end
function init
set(fig,'color',[1 1 1])
a=[1:3;1:3;1:3;1:3;1:3];
plot(a,'ko','MarkerSize',30,'Linewidth',2)
axis([0.5 5.5 0 4]);box off;axis off
end
function drawlines(gg,gg1)
init
ci=get(ed,'String');
if isempty(ci)
msgbox('Before pressing start you must insert the number inside the textbox')
return
end
if ((numel(ci)>5) | (numel(ci)<5))
msgbox('The number must have 5 digits')
return
end
c=[];
for f=1:5,c=[c str2num(ci(f))];,end
if ((max(c)>3) | (min(c)<1))
msgbox('The only alowed digits are 1,2 and 3')
return
end
set(butt,'Enable','off');
set(butt1,'Enable','off');
set(butt2,'Enable','off');
th = linspace(0,2*pi);
r = 0.22;
x = r*cos(th);
y = r*sin(th);
for b=1:4
patch(b+x,4-c(b)+y,'k')
pause(0.3)
line([b b+1],[4-c(b) 4-c(b+1)],'Color',[0 0 0],'LineWidth',5)
patch(b+x,4-c(b)+y,'w')
pause(0.3)
patch(b+1+x,4-c(b+1)+y,'k')
pause(0.3)
if b==4,patch(b+1+x,4-c(b+1)+y,'w'),end
if (~ishandle(fig)),return,end
end
set(butt,'Enable','on');
set(butt1,'Enable','on');
set(butt2,'Enable','on');
end
end
0 commentaires
Matt Tearle
le 25 Fév 2011
Doh. Too late. Still, how's this:
paths = [3,3,2,1,1;1,2,1,3,2;3,3,3,2,3];
[m,n] = size(paths);
th = linspace(0,2*pi);
r = 0.3;
x = r*cos(th);
y = r*sin(th);
for k=1:m
for j=1:n-1
line([j,j+1],[paths(k,j),paths(k,j+1)],'color','k','linewidth',2)
end
end
for k=1:m
for j=1:n
patch(j+x,k+y,'w')
line(j+x,k+y,'color','k','linewidth',2)
end
end
axis([0.5,n+0.5,0.5,m+0.5])
set(gca,'XTick',[],'YTick',1:m,'DataAspectRatio',[1 1 1],...
'YDir','reverse','Box','on','TickLength',[0,0])
If you like it, I can add animation.
EDIT: because I'm a dork, I added animation, whether you like it or not. So combining some of Paulo's ideas, which I thought were cute:
paths = [3,3,2,1,1;1,2,1,3,2;3,3,3,2,3];
[m,n] = size(paths);
pmax = max(paths(:));
h = zeros(n-1,m);
for k=1:m
for j=1:n-1
h(j,k) = line([j,j+1],[paths(k,j),paths(k,j+1)],...
'color','k','linewidth',2);
end
end
[x,y] = meshgrid(1:n,1:m);
line(x(:),y(:),'Linestyle','none','LineWidth',2,'Color','k',...
'Marker','o','MarkerSize',25,'MarkerFaceColor','w')
axis([0.5,n+0.5,0.5,m+0.5])
set(gca,'XTick',[],'YTick',1:m,'DataAspectRatio',[1 1 1],...
'YDir','reverse','Box','on','TickLength',[0,0])
set(h,'visible','off')
for k = 1:m*(n-1)
pause(0.5)
set(h(k),'visible','on')
end
1 commentaire
Konstantin
le 25 Fév 2011
2 commentaires
Paulo Silva
le 25 Fév 2011
We are crazy for new challenges because we learn alot from them and improve our matlab skills :)
Jiro Doke
le 25 Fév 2011
Just a little more fun... (adding on to Matt's and Paulo's ideas)
% paths = randi(5, [4, 6])
paths = ceil(5*rand(4, 6))
[m,n] = size(paths);
mx = max(paths(:));
set(gca, 'XLim', [0, n+1], 'YLim', [0, mx+1], 'YDir', 'reverse', ...
'Box', 'on', 'XTick', [], 'YTick', []);
h = line(NaN, NaN, 'LineWidth', 3, 'Color', 'Black');
[X,Y] = meshgrid(1:n, 1:mx);
line(X(:), Y(:), 'LineStyle', 'None', 'Marker', 'o', ...
'MarkerFaceColor', 'white', ...
'MarkerEdgeColor', 'black', 'MarkerSize', 20);
pt = line(NaN, NaN, 'Marker', 'o', 'MarkerEdgeColor', ...
'none', 'MarkerSize', 18);
for id1 = 1:m
title(num2str(paths(id1, :)));
for id2 = 1:n
set(h, 'XData', 1:id2, 'YData', paths(id1, 1:id2));
for id3 = 1:100
set(pt, 'XData', id2, 'YData', paths(id1, id2), ...
'MarkerFaceColor', 0.01*id3*[1 1 1]);
drawnow;
end
pause(0.5)
end
end
1 commentaire
Paulo Silva
le 25 Fév 2011
Nice and compact code, it gave me more ideas but I'm keeping the original problem size, I added the ball animation and a loop to the demo.
Voir également
Catégories
En savoir plus sur Animation dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!