2016-09-10 32 views
0

解释我的问题,我举一个例子:MATLAB进度条不depanding时间

让同我玩游戏,我有50Exp从120Exp,我拿我的一个新的水平。

我想制作一个单杠,显示50/120的酒吧绿色和其余的红色。就像进度条一样,但这取决于数值而不是Matlab时间计算。我想用Barh图表,但我不需要所有的轴和这样的东西。我希望它看起来像这样。

enter image description here

如果有人知道了,我想知道,

谢谢

+1

[**'waitbar' **](http://de.mathworks.com/help/matlab/ref/waitbar.html)? – thewaywewalk

回答

1

基本上你正在寻找的是:

画一个彩色矩形对另一彩色矩形。

在MATLAB中,一个彩色矩形可以axesbarsurfpatchimagerectangle,或annotation('rectangle'),仅举几例来呈现。 EXP栏可以用上述图形对象的组合来绘制。我会告诉你三种方法来做到这一点,但请记住,至少有十几种其他组合可供选择。

  1. 绘制红色部分('R')和绿色部分('G')为annotation('rectangle')优点:不需要绘制在轴上; 缺点:与其他方法相比,您对外观的控制可能较少。 (或surf,image)。 优点:更好的外观控制。如果您将G绘制为surfimage,则甚至可以获得3D着色和像素艺术效果。 缺点:轴线渲染有点贵。如果你有一堆这样的酒吧,它会造成显着的滞后。

  2. 绘制R和G堆叠在一个不可见轴上的barh优点:一次可以渲染多个小节。 缺点:必须绘制在轴上。控制条宽度的绝对值并不直观。

示例代码:您可以看到3种方法是如何动画的。

% Background axes. This is just for texts. 
main = axes('Position', [0, 0, 1, 1], 'Visible', 'off') 

% Method 1: Use a red annotation rectangle as background, and overlay a 
% green annotation rectangle on top. 
text(0.1, 0.9, 'Annotation BKG + Annotation Front', 'Parent',main) 
barbkg_an = annotation('rectangle', [0.1, 0.8, 0.8, 0.05], 'EdgeColor','black', 'FaceColor', 'red'); 
barfront_an = annotation('rectangle', [0.1, 0.8, 0.3*0.8, 0.05], 'EdgeColor','None', 'FaceColor', 'green'); 

% Method 2: Use a red axis as background, and draw green patch on it. 
text(0.1, 0.7, 'Axes BKG + Patch Front', 'Parent',main) 
barbkg_axes = axes('Position', [0.1, 0.6, 0.8, 0.05], 'Color', 'red', 'XColor', 'black', 'YColor','black','box','on','XTick',[],'YTick',[],'XLim',[0 1],'YLim',[0 1]); 
bar_pc = patch([0 0.3 0.3 0], [0 0 1 1], [0 1 0]); 

% Method 3: Draw "stacked" barh (so that both foreground and background are 
% drawn with one barh call) on an invisible axes 
text(0.1, 0.5, 'Axes BKG + Barh Front(X2)', 'Parent',main) 
barbkg_barh = axes; 
bar_barh = barh([0.2 0.4], [0.3 0.3; 0.7 0.7]', 0.25, 'stacked','ShowBaseline','off') 
colormap([0 1 0; 1 0 0]) 
set(barbkg_barh, 'Position', [0.1, 0, 0.8, 1], 'Visible','off','XLim',[0 1],'YLim',[0 1]); 

% Animate them 
for i = 0:0.01:1 
    set(barfront_an, 'Position', [0.1, 0.8, i*0.8, 0.05]); 
    set(bar_pc, 'XData', [0 i i 0]); 
    set(bar_barh(1), 'YData',[i i]) 
    set(bar_barh(2), 'YData',1-[i i]); 
    drawnow; 
end 

enter image description here

+0

有没有办法在补丁的中间有文字?因为我可以看到例如绿色/红色的百分比? – Ben

0

您可以使用barbarh,只是隐藏你不希望看到的轴元素。

例如:

function testcode() 
% Initialize GUI 
h.f = figure('MenuBar', 'none', 'NumberTitle', 'off', 'ToolBar', 'none'); 
h.ax = axes('Parent', h.f, 'Units', 'Normalized', 'Position', [0.1 0.45 0.8 0.1]); 

myxp = 10; 
totalxp = 120; 

h.currentxpbox = uicontrol('Parent', h.f, 'Style', 'edit', ... 
          'Units', 'Normalized', 'Position', [0.1 0.2 0.3 0.1], ... 
          'String', myxp, 'Callback', @(o,e)updatebar(h.f)); % Ignore usual callback inputs 
h.totalxpbox = uicontrol('Parent', h.f, 'Style', 'edit', ... 
         'Units', 'Normalized', 'Position', [0.6 0.2 0.3 0.1], ... 
         'String', totalxp, 'Callback', @(o,e)updatebar(h.f)); % Ignore usual callback inputs 

% For whatever reason, MATLAB needs 2 bars in order to stack, so we can 
% specify a dummy bar and render it outside of our axes limits 
tmph = barh([1, 2], [myxp, totalxp-myxp; 1, 1], 'stacked'); 

h.expbar_L = tmph(1); 
h.expbar_R = tmph(2); 
barwidth = h.expbar_L.BarWidth; 
h.ax.YLim = [1-barwidth/2 1+barwidth/2]; 

h.expbar_L.FaceColor = 'g'; 
h.expbar_R.FaceColor = 'r'; 

% Turn off axes ticks 
h.ax.XTick = []; 
h.ax.YTick = []; 

setappdata(h.f, 'handles', h); 
end 

function updatebar(fig) 
h = getappdata(fig, 'handles'); 
newxp = str2double(h.currentxpbox.String); 
totalxp = str2double(h.totalxpbox.String); 

h.expbar_L.YData = [newxp, 1]; 
h.expbar_R.YData = [totalxp - newxp, 1]; 
end 

给了我们如下:

yay

您可以操纵axes对象的大小和位置,以满足您的需求。