2011-03-20 202 views
18

有沒有可以添加到Matlab的圖形用戶界面,無論是 或uicontrol ActiveX組件一個現成的進度條uicontrol?如何將進度條控件添加到Matlab gui?

[編輯]我知道的waitbar功能,我的意思是可以落實到設計的GUI,而不僅僅是彈出窗口的組成部分。像狀態欄中的電池狀態。

+0

是不是隻適用於Internet Exploder的ActiveX? – Blender 2011-03-20 13:51:30

+2

Blender:除了您可以添加的給定uicontrols外,您還可以在Matlab的GUI設計環境(GUIDE)中使用ActiveX。 – 2011-03-20 14:09:14

+0

啊,好吧。作爲一名Linux人員,我只知道ActiveX有時用來利用IE。 – Blender 2011-03-20 14:17:09

回答

28

Waitbar及其變種顯示與狀態欄的彈出窗口。在大多數應用中,這是好的,使用起來非常簡單。

如果要整合現有的GUI窗口中的進度條,你有幾種選擇:

  1. 實現內部waitbar代碼 - 這實際上只是一個軸呈現彩色補丁的寬度取決於關於進度值。
  2. 使用標準java.swing.JProgressBar,可通過添加到您的Matlab的GUI內置javacomponent function
  3. 使用StatusBar utility或解釋here的進度欄添加到您的GUI窗口的狀態欄

所有這些選擇都適用於所有Matlab平臺。

+0

謝謝!這正是我所需要的。 – 2011-03-24 13:33:38

6

是的,有。 waitbar功能是你所需要的。那裏的例子很容易遵循,你可以馬上開始。它應該在所有3個平臺(Windows/OS X/Linux)上正常工作。

4

從這個MatLab Newgroup評論適應我的代碼,我可以整理出以下:

function h = uiProgressBar(varargin) 
%uiProgressBar: A waitbar that can be embedded in a GUI figure. 

    if ishandle(varargin{1}) && size(varargin, 2) > 1 
     ax = varargin{1}; 
     value = varargin{2}; 
     p = get(ax,'Child'); 
     x = get(p,'XData'); 
     x(3:4) = value; 
     set(p,'XData',x) 
     return 
    end 

    bg_color = 'w'; 
    fg_color = 'r'; 
    h = axes('Units','pixels',... 
     'XLim',[0 1],'YLim',[0 1],... 
     'XTick',[],'YTick',[],... 
     'Color',bg_color,... 
     'XColor',bg_color,'YColor',bg_color, ... 
     'Parent', varargin{1}); 
    patch([0 0 0 0],[0 1 1 0],fg_color,... 
     'Parent',h,... 
     'EdgeColor','none',... 
     'EraseMode','none'); 
end 

創作如下所示,其中parent是您希望將其添加到的父面板:

myProgressBar = uiProgressBar(parent); 

和更新進度條是如此簡單:

uiProgressBar(myProgressBar, .2); 

下面是使用一個完整的工作示例一個figure

f = figure('Name', 'Progress Bar Example', 'Position', [100 100 800 600]); 

progressBar = uiProgressBar(f); 

for i = 1:10:100 
    uiProgressBar(progressBar, i/100); 
    pause(.5); 
end 

ProgressBarLook

+0

這是我目前看到的我最喜歡的解決方案。謝謝!我接受了這個想法並大幅改變以適應我的需求,但最重要的是我讓我的課程成爲一門課。 – patrickvacek 2014-02-20 17:10:00

+0

這是目前爲止沒有頭痛的最佳答案。 – 2017-03-05 18:12:35

2

另一種簡單的解決方案是使用兩個嵌套uipanels像這樣:

function MyProgressBar(handle, progress) 
    % progress = 0.00001 .... 1 

    % 1st panel 
    p = uipanel(handle); 

    % 2n panel as bar 
    bar = uipanel(p); 
    set(bar, 'BackgroundColor', 'red'); 
    x = get(bar, 'Position'); 
    x(3) = progress;  % Corresponds to % progress if unit = normalized 
    set(bar, 'Position',x); 
end 

用法:

f = figure(); 
set(f,'Position',[100,100,400,40]); 
MyProgressBar(f, 0.5); % corresponds to 50% progress 
2

對任何人來說仍然有興趣,這裏是使用一類我的解決方案:

classdef progressbar < handle 
    properties(Access = protected) 
     h_panel   % Panel on which everything sits 
     h_ax   % The progress range axes 
     h_pbar   % The bar representing progress (patch) 
     h_ptext   % Percentage label 
    end 
    properties(Access = public, Dependent = true) 
     range   % Progress range 
     pvalue   % Current value 
     percent   % Percentage complete (relative within range) 
     position  % Position of the object (panel) 
     ax_tag   % Tag of the axes 
     visible   % Is the object (panel) visible? 
    end 
    properties(Constant = true) 
     default_color = [.75 .75 .9]; 
    end 
    methods 
     % Initializer 
     function obj = progressbar(fig, pos, range) 
      if nargin < 3 
       range = [0 1]; 
      end 
      obj.h_panel = uipanel('Parent', fig, 'Units', 'Inches', ... 
       'Position', pos, 'Tag', 'progbar_panel'); 
      obj.h_ax = axes('Parent', obj.h_panel, ... 
       'Units', 'Inches', 'Position', [0 0 obj.position(3) obj.position(4)], ... 
       'XTickLabel', '', 'XTick', [], 'YTickLabel', '', 'YTick', []); 
      obj.h_pbar = patch([range(1) range(1) range(1) range(1)], [0 0 2 2], ... 
       obj.default_color, 'Parent', obj.h_ax, 'Tag', 'progbar_patch'); 
      obj.h_ptext = text(obj.position(3)/2, obj.position(4)/2, '0%', ... 
       'Parent', obj.h_ax, 'FontWeight', 'bold', 'Units', 'Inches', ... 
       'HorizontalAlignment', 'center', 'Tag', 'progbar_text'); 
      obj.range = range; 
      obj.ax_tag = 'progbar_ax'; 
     end 

     % Property Access Methods 
     function set.range(obj, value) 
      % Instead of replotting, just reset the XLim to the 
      % extremities of the input range. If the values are not 
      % increasing, just default to [0 1]. 
      if value(end) > value(1) 
       set(obj.h_ax, 'XLim', value([1,end]), 'YLim', [0 2]); 
      else 
       set(obj.h_ax, 'XLim', [0 1], 'YLim', [0 2]); 
      end 
      % Reset progress. 
      obj.pvalue = value(1); 
     end 
     function value = get.range(obj) 
      value = get(obj.h_ax, 'XLim'); 
     end 
     function set.pvalue(obj, value) 
      % Expects a single value to represent progress value and 
      % constructs the selection rectangle from that. If multiple 
      % values are passed in, all are ignored but the last, since the 
      % left edge of the bar is always the first element of the 
      % range. 
      set(obj.h_pbar, 'XData', [obj.range(1) value(end) value(end) obj.range(1)], ... 
       'FaceColor', obj.default_color); 
      set(obj.h_ptext, 'String', sprintf('%3.0f%%', obj.percent * 100)); 
     end 
     function value = get.pvalue(obj) 
      % The progress bar is actually 2D, but we treat as if it is 1D. 
      % Hence the XData is actually an array of four values but we 
      % only consider the second (progress maximum). 
      limits = get(obj.h_pbar, 'XData'); 
      value = limits(2); 
     end 
     function set.percent(obj, value) 
      % Expects a single value between 0 and 1. 
      limits = obj.range; 
      obj.pvalue = value * (limits(2) - limits(1)) + limits(1); 
     end 
     function value = get.percent(obj) 
      limits = obj.range; 
      value = (obj.pvalue - limits(1))/(limits(2) - limits(1)); 
     end 
     function set.position(obj, value) 
      set(obj.h_panel, 'Position', value); 
     end 
     function value = get.position(obj) 
      value = get(obj.h_panel, 'Position'); 
     end 
     function set.ax_tag(obj, value) 
      set(obj.h_ax, 'Tag', value); 
     end 
     function value = get.ax_tag(obj) 
      value = get(obj.h_ax, 'Tag'); 
     end 
     function set.visible(obj, value) 
      if (isnumeric(value) && value >= 1) || strcmp(value, 'on') == 1 || strcmp(value, 'On') == 1 
       set(obj.h_panel, 'Visible', 'on'); 
      else 
       set(obj.h_panel, 'Visible', 'off'); 
      end 
     end 
     function value = get.visible(obj) 
      vis = get(obj.h_panel, 'Visible'); 
      value = strcmp(vis, 'on'); 
     end 

     % Public member functions 
     function increment(obj) 
      % Don't use this if the range is less than 1. 
      obj.pvalue = obj.pvalue + 1; 
     end 
     function display_text(obj, text, color) 
      if nargin == 3 && ~isempty(color) 
       set(obj.h_pbar, 'FaceColor', color); 
      end 
      set(obj.h_ptext, 'String', text); 
     end 
    end 
end 

聲明,像這樣一個實例:pb = progressbar(gcf, [1 1], [0 20]);

它可以相對使用或實際的數字,即pb.pvalue = 10;pb.percent = .5;在我的例子中做同樣的事情。

我的版本在顯示當前百分比的進度條中間有一個文本對象。

我的最新版本是here

-1

還有另外一種方式......很抱歉,如果提到了,我錯過了它。你可以建立一個圖形動態地添加一個酒吧的軸..它非常好地適用於自定義應用程序。彈出窗口總是迷路或阻礙。

+1

一個例子可以幫助解答這個問題 – 2016-12-07 18:49:53