Matlab代码5毫秒定时器

时间:2014-04-16 16:29:10

标签: matlab

能告诉我如何在matlab中编写5毫秒定时器代码吗?

%%分解成40个字节的数据包

%而时间< T1(= 5毫秒),继续填充40字节大小的数据包

    %while(Total_Connection_Time-Running_Time)>0   
    for n=1:length(total_number_of_bytes)
    % n=counter to go through "total_number_of_bytes" marix
    packets=[]; % 40-bytes matrix (packetization phase)

     % checking whether number of bytes at each talkspurt period is < or > 40 bytes in order to start packetization
    if (total_number_of_bytes(n)<=40)
    k=40-total_number_of_bytes(n); % calculating how many remaining bytes we need to complete a 40 bytes packet
    packets=[packets,total_number_of_bytes(n)+k];
    total_number_of_bytes(n)=40; %new bytes matrix after packetization (adding bytes from next talkspurt to get total of 40 bytes)
    total_number_of_bytes(n+1)= total_number_of_bytes(n+1)-k; % bytes are taken from the next talkspurt period in order to get a 40 byte packet

    if  total_number_of_bytes(n+1)<0
        for i=1:length(total_number_of_bytes) % looping through the array starting total_number_of_bytes(n+1)
            total_number_of_bytes(n+1)=total_number_of_bytes(n+1)+total_number_of_bytes(n+1+i)
            total_number_of_bytes(n+1+i)=0;
            packets=[total_number_of_bytes]
        end 
    end 
end

if(total_number_of_bytes(n)>40)
    m=total_number_of_bytes(n)-40; % cz we need 40 bytes packets
    packets=[packets,total_number_of_bytes -40];
    total_number_of_bytes(n)=40;
    total_number_of_bytes(n+1)= total_number_of_bytes(n+1)+m; % The remaining bytes are added to the next talkspurt period bytes
    packets=[total_number_of_bytes]
end 

2 个答案:

答案 0 :(得分:1)

为了更好的准确性使用

 java.lang.Thread.sleep(5);

而不是tic toc,请参阅更多here以获取更多信息。

答案 1 :(得分:1)

Tic和toc得到了不好的说唱,所以我会发布这个。

我尝试了以下内容:

tic
count = 0;
while toc<0.005
    a=randn(10);
    count = count+1;
end
toc

运行十次,toc的最大值为5.006 ms。每次计数约为1000次。

这与您的程序不同,但如果不涉及图形或GUI,我认为tic和toc可以完成这项工作。