% --- Executes just before mmmove is made visible.
function mmmove_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to mmmove (see VARARGIN)
% Choose default command line output for mmmove
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes mmmove wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = mmmove_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --------------------------------------------------------------------
function Debug_Callback(hObject, eventdata, handles)
% hObject handle to Debug (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function run_Callback(h, eventdata, handles)
% hObject handle to run (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
prompt={'误入速度v:','误入长度L:','误入半径r:'};
defans={'5','10','2'};
v=5;L=10;r=2;
p=inputdlg(prompt,'Input',1,defans);
v=str2double(p(1));
L=str2double(p(2));
r=str2double(p(3));
%guidata(h,handles)
if v<=0|L<=0|r<=0
warndlg('非法输入','警告')
else
ox1=r;oy1=r;ox2=r+L;oy2=r;
x1=r:0.015*v:r+L;
y1=2*r*ones(size(x1));
thita=0:0.015*v/r:pi;
x2=sin(thita)*r+ox2;
y2=cos(thita)*r+oy2;
x3=r+L:-0.015*v:r;
y3=zeros(size(x3));
x4=-sin(thita)*r+ox1;
y4=-cos(thita)*r+oy1;
x=[x1 x2 x3 x4 x1(1)];
y=[y1 y2 y3 y4 y1(1)];
plot(x,y);
axis([0,2*r+L,-1,2*r+1]);
h_text=text(0,-2,['L = ' num2str(L) ' , ' 'r = ' num2str(r) ' , ' 'v = ' num2str(v)]);
set(h_text,'fontSize',13);
axis equal
set(gca,'Visible','off')
hm=line(r,2*r,'color','red','marker','.','markersize',37,'erasemode','xor');
while 1
for i=1:length(x)
set(hm,'xdata',x(i),'ydata',y(i));
pause(0.0003)
drawnow
end
end
end
Z能让球不停的@环运动,我加了while 1的语句?br>q样在关闭窗口的时候,matlab的命令窗里就?x)蟩Z堆错误,大概是因为for循环里涉?qing)到的hm随着figure的关闭删除了Q但是程序仍然在q行Q所以蟩Z西说hm是invalid的。是不是可以放个停止E序的函数放figure或者axis的deleteFcn里头Q不q没扑ֈ那样的函数?br>期待好心大牛指教。。?br>q有是速度控制问题Q用计算的的速度控制Q运行明昑ց慢,大概是程序自p行也耗时吧,q个也不知道怎么办。不q后来看到篇文章说用timer对象来生动ȝQ很不错的样子?br>先引用下来:(x)
Q本文ؓ(f)Liuxqsmile原创Q{载请保留原文链接Q?br>MATLAB中提供了一些动态显C图形的例子Q例如vibes、truss{,但他们的E序l构都和由GUIDE产生的M文g的结构不同。truss中用while循环来更新图形窗口,pause函数来控制更新的速度。这L(fng)l构是不适合攑֜某一个子函数中来执行的,否则E序在执行该子函数时无法响应用户的其它操作,只能一开始就讄l止条gQ等待动ȝ自然l束?br>MATLAB中的定时器timer可以定时触发Q周期性地执行指定的函敎ͼ我们可以据此来实现绘囄自动更新Q而不?x)媄响整个GUI对用户其它操作的响应?br>比如我们要让一个曲面随旉周期性地变ŞQ类似vibes中的情ŞQ按?#8220;开?#8221;按钮后,曲面开始周期振动,同时我们可以调整振幅大小、周期的长短。所不同的是我们一切都在子函数中完成?br>在GUIDE中徏立界面元素后Q在“开?#8221;按钮的callback中徏立一个定时器timer对象object
handles.timer = timer('Period',0.05,'ExecutionMode','FixedRate',
... 'TimerFcn',{@PlotUpdate,handles});
Period是触发的周期Q这里设|ؓ(f)0.05sQExecutionMode是执行的方式Q可以有三种选择Q对于不需要精控制时序的动画的媄响不大,TimerFcn指定触发时所执行函数的句柄,在这里我们徏立一个函数PlotUpdate来执行绘囑֑令,handles作ؓ(f)参数传递到PlotUpdate中去?br>注意PlotUpdate的定义:(x)
function PlotUpdate(obj,events,handles)
前两个参数是必不可少的,最后的handles才是用户传递的数据。在q个函数中你基本上可以无视前两个参数Q按照曲面的q动势产生新的点坐标Q然后用set命o(h)新的坐标赋l曲?面片patch)对象的Vertices属性。drawnow更新可以了。你可以用曲面对象的UserData属性存攑ֽ前振动的q值相位等参数?br>然后?#8220;停止”按钮的callback中stop(handles.timer)停止定时器,可以停止自动绘图了?br>?#8220;增大振幅”按钮的callback中对振幅参数作修攏V?br>排除了一些小问题后,你的曲面l于可以动v来了Q按“增大振幅”{也没有影响囑Ş的运动,真的?#8220;一点媄响都没有”啊!
问题出在哪里呢?原来Q当定时器第一ơ触发时Q用h定的参数被传递到响应函数中执行,以后每一ơ触发,都是使用的这同一l参敎ͼq不?x)随着E序的运行而更新。好了,现在可以?br>“增大振幅”中重新赋一ơ参?br>set(handles.timer,'TimerFcn',{@PlotUpdate,handles})Q?br>怎么P辑ֈ你的要求了吧Q想知道更多QMATLAB Help的index中输入timerQ够多了吧?/p>
动画下蝲Q?span style="COLOR: red">下蝲后,用matlab7.0以上的版本可q行?/span>