[英]Draw rectangle based on data in matlab
我有如下一行數據:
0 -> 2 DATA 1.000000 - 1.000100 SUCCESS 1.000100 - 1.000200 FAIL
我想繪制如下數據:
我想我可以使用矩形,但是在x軸上不顯示時間? 如何解決呢?
如果成功,它將填充藍色,否則將填充紅色
好了,您應該使用fill命令而不是矩形命令。 請參閱下面的代碼以開始使用。 要根據自己的喜好進行調整,請查閱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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.