matlab 多个timer定时器为什么不是并行执行
答案:2 悬赏:0
解决时间 2021-03-21 22:51
- 提问者网友:冥界祭月
- 2021-03-21 03:15
是一个执行完了之后才执行另外一个,我在一个timer1的回调函数中设置了一个全局变量,调用两次后stop,delete了timer1,timer2,3中也是这么设置的,所以能看出是一个执行完才执行的另一个,但是timer不是并行的么,求解?
最佳答案
- 二级知识专家网友:傲娇菇凉
- 2021-03-21 04:53
看看申请的java功能能不能实现。
全部回答
- 1楼网友:滚刀废物浮浪人
- 2021-03-21 06:17
timer是一个函数,具体使用方法如下:
(1) timer 的定义
t=timer();
设置属性:
eg. set(t,'name','your_timer_name');
当然可以一次性设置完成:
例如:
tasktimer=timer(...
'name','febirdtimer',...
'timerfcn',@executetask,...
'errorfcn',@executeerror,...
'period',1,...
'executionmode','fixedrate');
这里timerfcn 为timer 执行的函数,后面的‘@excutetask’ 就是你定义的函数名
同样errorfcn 也是一样。
period 为执行周期,executionmode 为执行模式,fixedrate 为固定频率。当然前面所说的都是在这个前提之上。
(2) 关于timerfcn 的定义
当以timerfcn 的定义默认必须有两个参数
function excutetask(obj,eventdata)
% todo
end
其中obj 为执行该函数所对应的timer 对象,eventdata 为事件数据,一般里面为具体时间。
当需要在excutetask 中传入参数的时候,那么timer 可以这样定义:
那么这时函数定义应该为:
function excutetask(obj,eventdata,var1)
% todo
end
其他函数的定义也类似。
(3) 关于userdata
userdata 在timer 比较有用,因为当时用上面的方法传递参数是,matlab 只会在第一次传入参数。
所以我们可以在userdata 这个域中保存我们的数据。
例如:
t=[0];
lh=plot(t,sin(t),'-');
t=timer(...
'name','mytimer',...
'timerfcn',@executetask,...
'errorfcn',@executeerror,...
'period',1,'taskstoexecute',100,...
'executionmode','fixedrate');
ud=struct('linehandle',lh,'count',0);
set(t,'userdata',ud);
start(t);
function executetask(obj,eventdata,userdata) ;
ud=obj.userdata;
l=ud.linehandle;
c=ud.count;
t=get(l,'xdata');
y=get(l,'ydata');
t=[t c];
y=[y sin(0.1*c)];
set(ud.linehandle,'xdata',t,'ydata',y);
drawnow; %一般放置在set命令后,用于重构刷新图形。
ud.count=ud.count+1;
set(obj,'userdata',ud);
end
以上给出了一个使用timer 画图的方法。
(4) 关于timer 的函数
1.start();
2.stop();
3.timerfind();
eg.删除所有的timer
ts=timerfind;
if length(ts)>0
stop(ts);
delete(ts);
end
通过name 查找特定的timer:
t=timerfind('name','febirdtimer');
例如:
% --- executes on button press in pushbutton1.
function pushbutton1_callback(hobject, eventdata, handles)
% hobject handle to pushbutton1 (see gcbo)
% eventdata reserved - to be defined in a future version of matlab
% handles structure with handles and user data (see guidata)
tb= timer('name','buttontimer','startdelay', 4,'period', 4,'taskstoexecute', 2,...
'executionmode','fixedrate');
tb.startfcn = {'my_callback_fcn', 'my start message'};
tb.stopfcn = { @my_callback_fcn, 'my stop message'};
tb.timerfcn = @(x,y)disp('hello world!');
start(tb);
% --- executes on button press in pushbutton3.
function pushbutton3_callback(hobject, eventdata, handles)
% hobject handle to pushbutton3 (see gcbo)
% eventdata reserved - to be defined in a future version of matlab
% handles structure with handles and user data (see guidata)
tb=timerfind('name','buttontimer');
if length(tb)>0
stop(tb);
delete(tb);
end
function my_callback_fcn(obj, event, string_arg)%传入参数,前两个为默认参数
%其中event.type为回调函数类型,event.data为回调函数数据
txt1 = ' event occurred at ';
txt2 = string_arg;
event_type = event.type;%get type
event_time = datestr(event.data.time);%get timer period
msg = [event_type txt1 event_time];
disp(msg)
disp(txt2)
end
以上给出了通过gui button 按钮来控制timer开始和终止的方法。
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯