matlab进度条不依赖于时间

matlab progress bar not depanding on time

为了解释我的问题,我举个例子:

假设我正在玩游戏,我从 120Exp 到 50Exp 直到我达到下一个级别。

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

如果有人知道我想知道,

谢谢

您可以使用 bar or barh 并只隐藏您不想看到的轴元素。

例如:

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

给我们以下内容:

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

基本上你要找的是:

draw a colored rectangle over another colored rectangle.

在 MATLAB 中,可以使用 axesbarsurfpatchimagerectangle 渲染彩色矩形,或 annotation('rectangle'),仅举几例。可以使用上述图形对象的组合来绘制 EXP 柱。我将向您展示三种方法,但请记住,您至少还有十几种其他组合选择。

  1. 将红色部分('R')和绿色部分('G')都画成annotation('rectangle')优点:不需要画在坐标轴上; 缺点:与其他方法相比,您对其外观的控制可能较少。

  2. 将 R 绘制为 axes,将 G 绘制为 patch(或 surfimage)。 优点:更好的外观控制。如果你把G画成surfimage,你甚至可以获得3D阴影和像素艺术效果。 缺点: 轴的渲染成本有点高。如果你身边有一堆这样的酒吧,它会导致严重的滞后。

  3. 在不可见的轴上绘制堆叠 barh 的 R 和 G。 优点: 可以一次渲染多个条。 缺点: 必须绘制在坐标轴上。控制bar width的绝对值不直观

示例代码:您可以看到这 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