根据Matlab中的数据绘制矩形 [英] Draw rectangle based on data in matlab

查看:205
本文介绍了根据Matlab中的数据绘制矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下一行数据:

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

我想绘制如下数据:

我认为我可以使用矩形,但是它在x轴上不显示时间?该如何解决?

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

推荐答案

那么您应该使用fill命令而不是矩形命令.请参阅下面的代码,以帮助您入门.要根据自己的喜好进行调整,请查阅MatLab中关于我正在使用的功能的帮助文件.

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.

祝你好运,玩得开心!

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

这篇关于根据Matlab中的数据绘制矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆