【问题标题】:how to plot time stamp data from excel by matlab如何通过matlab从excel中绘制时间戳数据
【发布时间】:2014-04-08 16:02:37
【问题描述】:

我有一个包含 2000 个时间与幅度数据集的 excel 文件。时间单元为A,幅度单元为B。数据如下:

2/23/2012 3:12 -69.4

2/23/2012 3:13 -70.3

2/23/2012 3:14 -73.3

2/23/2012 3:15 -75.4

2/23/2012 3:16 -78.1

2/23/2012 3:17 -80.1

如何在 MATLAB 中绘制相对于时间戳的幅度值?

【问题讨论】:

    标签: excel matlab date plot timestamp


    【解决方案1】:

    代码

    %%// FILENAME
    filename = 'sid.xls';
    
    %%// OPTION 1: If there is a lot of data, XTickLaabels and XTicks would clutter up.
    %%// To Avoid that, define the number of XTickLabels needed, 
    %%// otherwise set it as NaN to use all x-data.
    XTickLabel_count = 3;
    
    %%// OPTION 2:  If you would like to show the time only with dates
    time_only = false;
    
    %%// OPTION 3:  If you would like to show the XTickLabels as 90 degrees rotated
    XTickRot = false;
    
    %%// Read in data
    [num,text1,raw] = xlsread(filename);
    
    %%// Account for 12AM times, which are not read in text1. Append that data.
    split1 = regexp(text1,'\s','Split');
    zero_times = cellfun(@numel, split1)==1;
    text1(cellfun(@numel, split1)==1) = mat2cell([char(text1(zero_times)) repmat(' 00:00:00',nnz(zero_times),1)],ones(1,nnz(zero_times)));
    
    %%// Get the time only data without dates
    split1 = regexp(text1,'\s','Split');
    split_text = vertcat(split1{:});
    time_text = split_text(:,2);
    
    %%// Use the time only data for XTickLabels
    if time_only
        text1 = time_text;
    end
    
    %%// Select few XTickLabels from the entire X-data or whole respectively
    %%// and store as text2
    if ~isnan(XTickLabel_count)
        XTickIntv = round(numel(text1)/XTickLabel_count);
        text2 = cell(size(text1));
        text2(1:XTickIntv:end)=text1(1:XTickIntv:end);
    else
        text2 = text1;
    end
    
    %%// Plot
    figure,plot(num)
    set(gca, 'XTickLabel',text2, 'XTick',1:numel(text2))
    if XTickRot
        xticklabel_rotate([],90,text2);
    end
    set(gca, 'Ticklength', [0 0]) %%// Remove XTicks but keep XTicklabels
    
    return;
    

    注意:此代码使用XTICKLABEL_ROTATE from Mathworks File-exchange

    方法一:使用XTickLabel_count = 3time_only = falseXTickRot = false

    方法2:使用XTickLabel_count = 5time_only = trueXTickRot = false

    方法3:使用XTickLabel_count = 10time_only = trueXTickRot = true

    【讨论】:

    • 嗨,当我在我的 MATLAB 中读取 excel 文件时,我从你的第一种方法中得到了这个:tinypic.com/view.php?pic=j8noqt&s=8#.U0SoA71lfDc 看到绘图创建了一些暗线而不是日期时间刻度。第二种方法描绘了这一点:tinypic.com/view.php?pic=15xqree&s=8 看到没有时间标记,只有一些整数值。您的第三种方法绘制类似于第一种方法的内容 + 它还在以下行显示错误消息:4 这里有什么问题?
    • 嗨,这是我的原始 excel 文件。你能告诉我用你的代码绘图有什么问题吗? drive.google.com/file/d/0ByntE7ZqdSzoNkRhb0p3ckl0Mzg/…
    • @user3511734 查看已编辑的代码并确保您选择了脚本以从 here 旋转 XTickLabels
    • @user3511734 想知道这是否适合您,因为答案中的图表来自您的数据。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-25
    • 1970-01-01
    • 2021-08-14
    • 2022-10-07
    • 2021-11-23
    • 2016-01-14
    • 2018-10-21
    相关资源
    最近更新 更多