Introduction

Matlab Logo While matlab is running through a long function or for loop, the user has no idea when it will be completed. By implementing a progress bar, the user will be able to see the status of the function and when it will be completed. In this tutorial, you will learn how to implement a progress bar to your GUI, script, or function. This command is typically used inside a for loop that performs a lengthy computation. While it is possible to use the waitbar command in matlab, it is not as flexible as this custom progress bar for the following three reasons:

  1. Does not show percent completed

  2. Does not show time left before completion

  3. If user closes wait bar before it is completed, an error will result

Matlab Wait Bar:

Progress Bar Modified

Modified Wait Bar:

Matlab Wait Bar

How to use the Progress Bar

  1. Download the m-file here by right clicking on the link and selecting the “Save as” option (This code was originally deveopled by Steve Hoelzer and you can find the original m-file here).

  2. Place the m-file in whatever folder you desire and set the Matlab Current directory to wherever you saved the m-file.

  3. The progressbar function takes in two arguments. The first is the fraction of the task that is completed, so the input is between 0 and 1. The second argument is for positioning purposes:

    Position determines where the progressbar figure appears on screen. The progress bar’s position can be specifed as follows:

    • [x, y] - Position of lower left corner in normalized units (0.0 - 1.0)
    • 0 - Centered (Default)
    • 1 - Upper right
    • 2 - Upper left
    • 3 - Lower left
    • 4 - Lower right
    • 5 - Random [x, y] position

    The output parameter is used mainly to break from the for loop if necessary. While the progress bar is active on the screen, the output parameter is equal to 0. When the user closes the progress bar, the function returns 1.

  4. Now, lets run a quick example using the progress bar. Cut and paste the following code into the the Matlab command prompt

    clear all
    for x=1:100000
     
    stopBar= progressbar(x/100000,0);
     
    if (stopBar) break; end
     
    end
  5. Here’s what you should see:

    Progress Bar Modified

  6. If you try to close the progress bar before it gets to 100% you will receive the following prompt:

    Confirm Stop

    If you decide to close the bar, it will cancel the bar and will exit the for loop, stopping all computations within that for loop.

  7. If you don’t like the color of the progress bar, you can modify the code to change its color. The line you are looking for is on line 190. You can mess around with the number array to give you the desired color:

    190
    
    set(progpatch,'FaceColor',[.1 1 .1]);
  8. If you’re using this progress bar in conjunction with a GUI, you should check out this post on how to disable buttons. It works great with the progress bar because it doesn’t allow the user to push any buttons while the data is being processed.

This is the end of the tutorial.