2011-03-20 37 views
18

¿Hay un uicontrol de barra de progreso ya preparado que se puede agregar a Matlab gui, uicontrol o componente ActiveX?¿Cómo agregar control de barra de progreso a Matlab gui?

[edit] Sé sobre la función de la barra de espera, me refiero a un componente que se puede implementar en la GUI diseñada y no solo salir de la ventana. Algo así como el estado de la batería en la barra de estado.

+0

¿No es ActiveX solamente para Internet Exploder? – Blender

+2

a Blender: puede usar ActiveX en el entorno de diseño GUI de Matlab (GUIDE) además de los controles de usuario que puede agregar. –

+0

Ahh, está bien. Siendo una persona de Linux, solo sabía que ActiveX a veces solía explotar IE. – Blender

Respuesta

28

La barra de espera y sus variantes muestran una ventana emergente con una barra de estado. En la mayoría de las aplicaciones, esto está bien y es muy fácil de usar.

Si desea integrar una barra de progreso dentro de una ventana de interfaz gráfica de usuario existente, usted tiene varias opciones:

  1. implementar el código waitbar interna - esto es realmente sólo un eje que presenta un parche de color cuya anchura depende en el valor de progreso.
  2. utilizar el estándar java.swing.JProgressBar, que se puede añadir a su Matlab GUI a través de la incorporada en el javacomponent function
  3. utilizar el StatusBar utility o la explicación here añadir una barra de progreso de la barra de estado de la ventana de interfaz gráfica de usuario

Todas estas opciones funcionan en todas las plataformas Matlab.

+0

Gracias! eso era exactamente lo que necesitaba. –

6

Sí, la hay. La función waitbar es lo que necesita. Los ejemplos son fáciles de seguir y puede comenzar de inmediato. Debería funcionar bien en las 3 plataformas (Windows/OS X/Linux).

4

Adaptación mi código de este MatLab Newgroup comentario, yo era capaz de armar el siguiente:

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 

Creación es el siguiente, donde parent es el panel principal al que desea agregarlo:

myProgressBar = uiProgressBar(parent); 

y la actualización de la barra de progreso es tan simple como esto:

uiProgressBar(myProgressBar, .2); 

Aquí está un ejemplo de trabajo completo utilizando un 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

Esta es mi solución favorita que he visto hasta ahora. ¡Gracias! Tomé la idea y la cambié sustancialmente para satisfacer mis necesidades, pero lo más importante es que convertí mi versión en una clase. – patrickvacek

+0

Esta es la mejor respuesta hasta ahora sin dolor de cabeza. –

2

otra solución simple es utilizar dos uipanels anidados como este:

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 

Uso:

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

Para cualquier persona todavía está interesado, aquí está mi solución usando una clase:

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 

Declarar una instancia de este modo: pb = progressbar(gcf, [1 1], [0 20]);

Se puede utilizar con relativa o los números reales, es decir, pb.pvalue = 10; y pb.percent = .5; hacen lo mismo en mi ejemplo.

Mi versión presenta un objeto de texto en el medio de la barra de progreso que muestra el porcentaje actual.

Mi última versión está disponible here.

-1

Hay otra manera ... Lo siento si fue mencionada y me la perdí. Puede construir una figura agregando dinámicamente los ejes para una barra. Funciona muy bien y es fácilmente adaptable para aplicaciones personalizadas. Las ventanas emergentes siempre se estaban perdiendo o en el camino.

+1

Un ejemplo ayudaría a esta respuesta –

Cuestiones relacionadas