【问题标题】:Matlab plot of categorical x-axis and cell array data分类 x 轴和元胞数组数据的 Matlab 图
【发布时间】:2020-03-31 15:45:47
【问题描述】:

我有一个带有嵌套元胞数组的元胞数组:

我想按行绘制每个嵌套单元格数组。但并非所有嵌套元胞数组都是 8x1。我需要将 empy 值填写为 NaN 或零,但仍然能够连续绘制数据。

第 7-9 列的示例:

Column7   Column8   Column9
1          1           1                       
2          2           2                        
3        NaN          NaN
4        NaN          NaN
5        NaN          NaN
6        NaN          NaN

我想按行绘制,第1行是(1,1,1),第2行是(2,2,2),第3行是(3, NaN, NaN),以此类推; 因此,类别 7 的垂直值将是 1-6。类别 8 将具有垂直值 1 到 NaN,但仅绘制值 1 和 2。

我希望第 1 行的值通过线路连接:

例子:

figure
hold on
cellfun(@(C1) plot(cell2mat(C1,:), 'o-'), C);
% setup axes
xlim([0, 15]);
ax = gca;
ax.XTick = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
ax.XTickLabel = {'1','2','3','4','5','6','7','8','9','10','11','12','13','14','15'};

【问题讨论】:

    标签: matlab plot cell-array


    【解决方案1】:

    最简单的方法是构建一个二维矩阵,其中包含您的数据(如果存在)和其他地方的 NaN。 Matlab 中的大多数绘图命令都会忽略 NaN。

    要将元胞数组转换为矩阵,您可以创建一个仅包含 NaN 的矩阵,并逐列填写当前数据。例如;

    % Create dummy data
    C = {[1;2;3;4], [7;7;8], [5;4]};
    % Find the maximum number of rows possible across all cells
    Nrows = max(cellfun(@length, C));
    % Create matrix full of NaNs
    M = nan(Nrows, length(C));
    % Loop cells
    for i = 1 : length(C)
        % Pull the contents of this cell
        Content = C{i};    
        % Fill this column with as many rows as we found
        M(1 : length(Content), i) = Content;
    end
    

    生成的矩阵M 包含C 中每个单元格的值,每列一个。然后,您可以简单地绘制它们

    plot(M)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-03
      • 1970-01-01
      • 1970-01-01
      • 2013-10-03
      • 1970-01-01
      • 1970-01-01
      • 2013-08-09
      相关资源
      最近更新 更多