首页 文章

以格式化时间(min.sec)绘制关于分钟的绘图

提问于
浏览
5

我试图在Matlab中绘制一些关于分钟而不是秒的数据作为格式化时间,即 min.sec .

我有实时数据流在每个样本收到的地方,它的时间也以秒为单位发送 . 然后我就时间绘制它们 . 现在,由于我的 Session 时间大约是15分钟,所以我不能在时间上进行策划 . 因此我想根据时间绘制它( min.sec ) . 我尝试将接收时间除以60,但是这给了我100分钟而不是60分钟(在0.9999之后的分钟增量而不是0.59分钟) . 如何转换它以便我能够在几分钟内绘制时间图?

这就是我所说的0.99分钟而不是0.59分 . 正常分钟有60个分度而不是100.
enter image description here

EDIT: 我尝试了m7913d的建议,这就是我得到的 .

  • 首先我在不改变刻度的情况下以秒为单位绘制信号(A normal plot(t,v)

  • 我在图中添加了 datetick('x', 'mm:ss'); (Matlab 2015b不支持Xtick格式)

以下是结果的屏幕截图
enter image description here

以秒为单位的时间最多为80秒,当转换为分钟时,它应该给我1分20秒作为最大x轴限制 . 但这种情况并非如此 . 我试图构造一个t向量(即像 t=0:seconds(3):minutes(3) ),但我无法将它链接到我的秒向量,当从串口接收到新样本时,它将不断更新 . 谢谢

1 回答

  • 1

    您可以使用xtickformat指定x标签的所需格式,如下所示:

    % generate a random signal (in seconds)
    t = 0:5:15*60;
    y = rand(size(t));
    
    plot(seconds(t),y) % plot your signal, making it explicit that the t is expressed in seconds
    xtickformat('mm:ss') % specify the desired format of the x labels
    

    请注意,我使用seconds方法返回duration对象,以向Matlab指示 t 以秒表示 .

    上述脚本的输出是(右图像是左图像的缩放版本):

    enter image description here

    enter image description here

    Pre R2016b

    可以使用 datetime 而不是 xtickformat ,如下所示:

    datetimes = datetime(0,0,0,0,0,t); % convert seconds to datetime
    plot(datetimes,y)
    datetick('x', 'MM:SS'); % set the x tick format (Note that you should now use capital M and S in the format string
    xlim([min(datetimes) max(datetimes)])
    

相关问题