2016-09-10 27 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對象的大小和位置,以滿足您的需求。