【问题标题】:How do I plot vectors of different length on the same axes in MATLAB?如何在 MATLAB 的同一轴上绘制不同长度的向量?
【发布时间】:2009-10-30 03:16:36
【问题描述】:

我在尝试比较和绘制两个不同长度的文件时遇到了麻烦。在 MATLAB 中,我不知道如何在同一个 x 轴上绘制两个不同长度的向量。由于一个文件丢失了一些数据,我想创建一行并为相应的时间戳放置空白。以下是数据文件示例:

文件 1:

date time  T01  T02  T03  T04  T05
8/16/2009 0:00, 516.900024, 450.5,      465.200012, 546.799988, 539.700012
8/16/2009 0:10, 351.200012, 398.899994, 418.100006, 510.299988, 518.5
8/16/2009 0:30, 241.399994, 252.399994, 256,        360.600006, 386.5
8/16/2009 1:00, 184.199997, 154.300003, 143.899994, 236.600006, 244.399994

文件 2:

date time  T01  T02  T03  T04  T05
8/16/2009 0:00, 656.799988, 611.200012, 860.599976, 604.700012, 288.5
8/16/2009 0:10, 527.400024, 359.200012, 789.099976, 789.099976, 446.799988
8/16/2009 0:20, 431.5,      327.100006, 763.599976, 895.099976, 689.099976
8/16/2009 0:30, 328.399994, 301.700012, 824,       1037.099976, 955.299988
8/16/2009 0:40, 261.5,      332.200012, 811.700012, 962.200012, 915.599976
8/16/2009 0:50, 180.300003, 291.100006, 700.099976, 855.200012, 836.900024
8/16/2009 1:00, 294.399994, 281.399994, 731.299988, 881.700012, 666.200012
8/16/2009 1:10, 274.899994, 334.200012, 759.400024, 913.900024, 760.799988

我正在尝试重新制作文件 1,如下所示:

8/16/2009 0:00, 516.900024, 450.5,      465.200012, 546.799988, 539.700012
8/16/2009 0:10, 351.200012, 398.899994, 418.100006, 510.299988, 518.5
8/16/2009 0:20, ,,,,
8/16/2009 0:30, 241.399994, 252.399994, 256,        360.600006, 386.5
8/16/2009 0:40, ,,,,
8/16/2009 0:50, ,,,,
8/16/2009 1:00, 184.199997, 154.300003, 143.899994, 236.600006, 244.399994
8/16/2009 1:10, ,,,,

有没有办法做到这一点?当我尝试根据时间戳链接 2 个文件时,这对我有很大帮助。

p.s.:我正在尝试在 MATLAB 中使用 ismember 函数,但遇到了各种问题。

【问题讨论】:

    标签: file matlab timestamp


    【解决方案1】:

    我假设您首先从answers to this question 中描述的文件中读取数据。如果您有时间戳(使用DATENUM 转换)和存储在变量fileData1fileData2 中的两个文件中的数据,以下是在同一组轴上绘制每个数据的简单方法(使用函数PLOTHOLD 命令):

    t1 = fileData1(:,1);  %# Time-stamps for file 1
    d1 = fileData1(:,2);  %# First column of data for file 1
    plot(t1,d1,'ro-');    %# Plot data from file 1, using a red line with circles
    hold on;
    t2 = fileData2(:,1);  %# Time-stamps for file 2
    d2 = fileData2(:,2);  %# First column of data for file 2
    plot(t2,d2,'bo-');    %# Plot data from file 2, using a blue line with circles
    

    上图中的每条线都有不同数量的时间点(即圆形标记),但每条线都是连续的(即不间断的)线。如果要在图中显示缺少时间戳的中断,可以使用 NaN 值填充文件 1 中的数据。如果您确定较小文件中永远不会有时间戳不在较大文件中(即较小文件中的时间戳是时间戳的子集在较大的),那么你可以使用函数ISMEMBER如下:

    newData = nan(size(fileData2));      %# New file 1 data, initialized to NaN
    t = fileData2(:,1);                  %# Use the time-stamps from file 1
    index = ismember(t,fileData1(:,1));  %# Find row indices of common time-stamps
    newData(:,1) = t;                    %# Copy time-stamps
    newData(index,:) = fileData1;        %# Copy data
    

    如果较小的文件中有个时间戳,而较大的文件中没有,那么您将不得不使用基于INTERSECT 函数的解决方案(如my answer to your other related question 中所示)。

    现在,您可以使用以下方法绘制数据:

    d1 = newData(:,2);            %# First column of padded data for file 1
    d2 = fileData2(:,2);          %# First column of data for file 2
    plot(t,d1,'ro-',t,d2,'bo-');  %# Plot both lines
    

    以下是 NaN 填充在绘图时产生的差异示例(使用一些随机生成的数据):

    【讨论】:

    • 非常感谢,正如您从我的另一个问题中看到的,您也发表了评论,我花了很多时间来解决上述问题。
    • @gnoice :有没有办法绘制单元格格式的矩阵?通常我使用 > plot(x,y) x= data(:,1); y=数据(:,2);当数据在单元格中时,我该怎么做?我试过cellplot,没有用。也试过 x=cell2mat(data(:,1)) ;也没有用。
    • @AP:这取决于你的变量data 的样子。它是一个 N×2 单元阵列,每个单元有一个数字吗?或者它是一个 1×2 元胞数组,其中每个元胞都包含一个 N×1 向量?第一种情况可以这样处理:plot([data{:,1}],[data{:,2}]); 第二种情况可以这样处理:plot(data{1},data{2}); 没有更多关于data 的详细信息,这是我能回答的最好的。
    • @gnoice:它是一个 N,M 矩阵,我通过编写一个 xlsfile 并将其重新读取为 data=xlswrite(' ') 来使其工作;这似乎是一种关于这样做的方式。使用这两个命令中的任何一个,它都会给出相同的错误“无效的第一个数据参数”
    • @AP:啊哈,我现在明白了。问题可能是您的元胞数组file 的第一行中有字符串。这些字符串导致您遇到的错误。您必须只选择那些具有数值的单元格进行绘图。如果您的字符串只在file 的第一行,这应该可以工作:plot([file{2:end,1}],[file{2:end,2}]);
    猜你喜欢
    • 1970-01-01
    • 2017-07-21
    • 1970-01-01
    • 2013-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多