Draw rectangle based on data in matlab

我与影子孤独终老i 提交于 2019-12-13 06:58:51

问题


I have one row data as follows :

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

I want to plot data as below :

I think I can use rectangle but it does not show time at x-axis? How to solve this?

if SUCCESS it will fill blue color, otherwise red color


回答1:


Well you should use the fill command instead of the rectangle command. See my code below to get you started. To tweak things to your liking just consult the help file in MatLab on the functions I am using.

Good luck and have fun!

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


来源:https://stackoverflow.com/questions/24904965/draw-rectangle-based-on-data-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!