根据matlab中的数据绘制矩形

时间:2014-07-23 08:04:28

标签: matlab text label rectangles

我有一行数据如下:

 0 -> 2 DATA 1.000000 - 1.000100 SUCCESS 1.000100 - 1.000200 FAIL

我想绘制数据如下:

enter image description here

我想我可以使用矩形,但它不会在x轴上显示时间?怎么解决这个问题?

如果SUCCESS它将填充蓝色,否则为红色

1 个答案:

答案 0 :(得分:1)

那么你应该使用fill命令而不是rectangle命令。请参阅下面的代码以帮助您入门。要根据自己的喜好调整内容,请参阅MatLab中有关我正在使用的功能的帮助文件。

祝你好运,玩得开心!

clear all
clc
close all

% Declare input string
input_str = '0 -> 2 DATA 1.000000 - 1.000100 SUCCESS 1.000100 - 1.000200 FAIL';
% Analyse string by using textscan, the columns of your data is stored in
% cells C{n}, where n denotes the column
C = textscan(input_str,'%d -> %d DATA %f - %f %s %f - %f %s');

% Declare data labels
data_label = cell(length(C{1}),1);
for ii=1:length(C{1})
    data_label = ['DATA ',num2str(C{1}(ii)),'->',num2str(C{2}(ii))];
end

% Draw the rectangle, first define a rectangle height
r_height = 0.00002;

figure(1)
% Plot all elements in same figure and save all elements plotted
hold on
% Show the grid
grid on

for ii=1:length(C{1})
    % Rectangle 1
    % Declare vertices
    xs=[C{3}(ii) C{4}(ii) C{4}(ii) C{3}(ii)];
    ys=[0 0 r_height r_height];
    % Determine color
    if(strcmp(C{5}(ii),'FAIL'))
        rgb_color = [1 0 0]; % Red
    else
        rgb_color = [0 0 1]; % Blue
    end
    % Plot rectangle
    fill(xs,ys,rgb_color)
    % Rectangle 2
    % Declare vertices
    xs=[C{6}(ii) C{7}(ii) C{7}(ii) C{6}(ii)];
    ys=[0 0 r_height r_height];
    % Determine color
    if(strcmp(C{8}(ii),'FAIL'))
        rgb_color = [1 0 0]; % Red
    else
        rgb_color = [0 0 1]; % Blue
    end
    % Plot rectangle
    fill(xs,ys,rgb_color)

    % Force to use the same axis scale:
    axis equal;

    % Set the limits
    % Create offset value
    offsetval = 0.1*(C{7}(ii)-C{3}(ii));

    % Set x limites
    xlim([C{3}(ii)-offsetval C{7}(ii)+offsetval])

    % Finally plot the labels
    text(C{3}(ii),r_height/2,data_label,'Color',[1 1 1])
    text(C{6}(ii),r_height/2,data_label,'Color',[1 1 1])

end