【问题标题】:clock Time on x-axis in matlabmatlab中x轴上的时钟时间
【发布时间】:2014-05-16 20:17:28
【问题描述】:

我需要一些与 MATLAB 中的绘图相关的帮助。

我想在 x 轴上绘制关于时钟时间的数据。我每 15 分钟有一个占用信息数据。我想按时间绘制它。我该怎么做?问题在于 x 轴,我如何处理时间和均匀间隔,例如数据的形式是

data=[1 0 0 0 0 1 1 1 1 0 0 0 .............]

时间值是从上午 9 点到晚上 9 点,间隔是 15 分钟

如何设置 x 轴上的间隔?

谢谢

【问题讨论】:

  • 最简单的方法:给自己定义一个时间向量:timeSpan=9:0.25:21使用 24 小时制非常容易。否则会变得更加困难,因为您的数据会在 12 点钟后跳回 0.25。定义等于从 9 到 21,步长为 0.25。如果您有更长的时间,请考虑将经过的时间用作 x 比例。但老实说,这并没有解决问题,而是忽略了它;)

标签: matlab time plot


【解决方案1】:

下面的代码解决了这个问题。您输入整数值作为开始小时和分钟、结束时间和测量之间的时间步长。进一步输入/更改steps_x 值。这显示了在 x 轴上跳过了多少时间值。 7=skip6 值。 for-loop 下面是我的 y 数据。我刚刚使用了random-function
生成的 x 轴是一个单元阵列。对于某些应用程序来说,这可能是一个问题。此外,我使用了一些“不必要的”变量。这些我用于验证我的代码。我没有改变它,因为我认为这样更容易理解。
我最大的问题

clc; clear all; close all;
%%//Variable declaration
start_hour = 9; %in hours
start_min = 5;     %//in minutes
steps_min = 10;    %//in minutes
end_hour = 21.0;   %//in hours
end_min= 0;        %//in minutes
steps_x = 7;       %//how many times are displayed on the axis, doesn't change data

%%// code for computing internal values, steps and transforming to am/pm
%//changes the entries above to hour display(9.75=9h45min)
start_time= (start_hour+start_min/60); 
%//changes the entries above to hour display(9.75=9h45min)
end_time=(end_hour+end_min/60); 
%//changes steps to hour
steps_hour= steps_min/60;             
%//array of hours
mytest_timeline = start_time:steps_hour:end_time;
%//array of minutes(copied and modified from @natan)
mytest_minutes = mod((0:steps_min:(steps_min*(length(mytest_timeline)-1)))+start_min,60);
%//cell array for am/pm display
mytest_timeline_ampm = num2cell(mytest_timeline); 
%//if hour is smaller 12 write am otherwise pm
for k=1:length(mytest_timeline); 
    if mytest_timeline(k) < 12
        mytest_ampm = {'am'}; 
        %//converting the down rounded hour to str
        helper_hour=num2str(mod(floor(mytest_timeline(k)),12));
        %//converting the minute to str and giving it 2 digits eg. 05 for 5min
        helper_minute = num2str(mytest_minutes(k),'%02d'); 
        %//joining strings
        mytest_timeline_ampm(k)= strcat(helper_hour,{'.'}, helper_minute, mytest_ampm);
    %//same as for am just for pm
    else 
        mytest_ampm = {'pm'};
        helper_hour=num2str(mod(floor(mytest_timeline(k)),12));
        helper_minute = num2str(mytest_minutes(k),'%02d');
        mytest_timeline_ampm(k)= strcat(helper_hour,{'.'}, helper_minute, mytest_ampm);
    end
end 

%%// generated y data so that i could test the code
mytest_y = rand(size(mytest_timeline)); 

%%// changing display
%//x-coordinates(for displaying x,y)
mytest_x= 1:length(mytest_timeline); 
%//x-axis label
mytest_x_axis = 1:steps_x:length(mytest_timeline); 
%//plots the data mytest_y in uniform distances (mytest_x)
plot(mytest_x, mytest_y, 'b')
%//changes the x-label accordingly to mytest_x_axis to the am/pm timeline
set(gca, 'XTick',mytest_x_axis, 'XTickLabel',mytest_timeline_ampm(mytest_x_axis))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-21
    相关资源
    最近更新 更多