Last week, I described the built-in PopupPanel object, and showed how it can be used to present popup messages with HTML content and even entire webpages. I explained that PopupPanel uses an internal browser object to achieve this. In fact, Matlab’s browser object predates PopupPanel by many years and quite a few releases. This browser object can be used as a stand-alone component that we can easily embed in our Matlab GUI applications.
Here is a simple example in which a Matlab Listbox uicontrol is used to select the contents of an adjacent browser component:
% Create a blank figure window f=figure('Name','Browser GUI demo','Num','off','Units','norm'); % Add the browser object on the right jObject = com.mathworks.mlwidgets.html.HTMLBrowserPanel; [browser,container] = javacomponent(jObject, [], f); set(container, 'Units','norm', 'Pos',[0.3,0.05,0.65,0.9]); % Add the URLs listbox on the left urls = {'www.cnn.com','www.bbc.co.uk','myLocalwebpage.html',... 'www.Mathworks.com', 'UndocumentedMatlab.com'}; hListbox = uicontrol('style','listbox', 'string',urls, ... 'units','norm', 'pos',[0.05,0.05,0.2,0.9], ... 'userdata',browser); % Set the listbox's callback to update the browser contents cbStr=['strs = get(gcbo,''string''); ' ... 'url = strs{get(gcbo,''value'')};' ... 'browser = get(gcbo,''userdata''); ' ... 'msg=[''<html><h2>Loading '' url '' - please wait''];'... 'browser.setHtmlText(msg); pause(0.1); drawnow;'... 'browser.setCurrentLocation(url);']; set(hListbox,'Callback',cbStr);
In this simple example, we can see how the Java browser object can easily be controlled by Matlab. Specifically, we use two modes of the browser: first we present an HTML message (‘Loading www.cnn.com – please wait‘) and then replacing this content with the actual webpage, if accessible. If the webpage is not accessible, an error message is displayed:
We can easily expand this simple example to display any HTML message or webpage, in a seamless integration within our GUI.
Now, who ever said that Matlab GUI looks static or boring???
In an unrelated note, I would like to extend good wishes to Ken Orr, who has left the Mathworks Desktop development team to join Apple a few days ago. You probably know Ken from his good work on the Desktop and the official Matlab Desktop blog. Hopefully, in his new position Ken will be able to influence Mac Java in a way that will reduce the numerous recurring issues that afflict Matlab Mac releases.
Related posts:
- GUI integrated HTML panel Simple HTML can be presented in a Java component integrated in Matlab GUI, without requiring the heavy browser control....
- Inactive Control Tooltips & Event Chaining Inactive Matlab uicontrols cannot normally display their tooltips. This article shows how to do this with a combination of undocumented Matlab and Java hacks....
- Matlab-Java interface using a static control The switchyard function design pattern can be very useful when setting Matlab callbacks to Java GUI controls. This article explains why and how....
- Setting listbox mouse actions Matlab listbox uicontrol can be modified to detect mouse events for right-click context menus, dynamic tooltips etc....
- Customizing Matlab labels Matlab's text uicontrol is not very customizable, and does not support HTML or Tex formatting. This article shows how to display HTML labels in Matlab and some undocumented customizations...
- The javacomponent function Matlab's built-in javacomponent function can be used to display Java components in Matlab application - this article details its usages and limitations...




Thanks Yair! I’ll do what I can to help influence Java on the Mac.
Thanks for a great tip! I have been struggling with the limited HTML capabilities of the JEditorPane but now you gave me a solution that really works out of the box.
Hi Yair,
Is it possible to view non-html links, say, a pdf file from within the matlab web browser? That should be without opening a new acrobat reader window.
Thanks
Sid
@Sid – I don’t think so. Even regular browsers need to use Acrobat (or another PDF reader like Foxit) to display PDF.
Hi Yair,
I’m designing a dialog similar to a throbber, indicating that some computation is running, but autonomous, contrary to matlab’s native waitbar. In order to save CPU ressources, i try to take advantages of animated GIF. After having considered timer functions for refreshing an axes, i finaly came out to use an HTML browser that can render basic animated GIFs. Your tip provides me a great solution, however, i’m wondering how i can remove the scrollbars that appear when i set the url to any image file :
I can’t see any method dealing with scrolling by typing :
I will try to work on browser size, but i appreciate if you can provide some hints.
Thanks.
Arnaud
@Arnaud – here’s a much lighter-weight alternative:
Hi Yair,
I’m just reading your response to my post I wrote… 2 years ago.
The need for a throbber was off the radar during this time, but finaly rises again, and your piece of code is actualy exactly what I was looking for.
Thank you for your help, and continue to keep this extremly usefull website alive.
Arnaud
Hi,
This works great.
I do have a slight problem – if the page I’m viewing has some dhtml pop up window, it shows like an error message and there’s nothing I can do – the whole program is stuck until I turn it off (click ok or something).
Here’s an example of such a web page:
http://www.domainscalping.com/ (simply let the page load, and then refresh the screen to see the popup).
Is there a way to monitor such messages and handle them via the code? Or maybe disable them?
Thanks
Pingback: Customizing menu items part 3 | Undocumented Matlab
Hi, Yair!
Is it possible to track changes in browser object? For example, change listbox when we follow a link to another site.
Thanks.
Ivan.
@Ivan – the easiest way would be to check whether the browser object (or one of its sub-components) has a built-in event (Matlab callback) that you can use. Alternatively, you could try using a property-change listener as explained here.
I am having an issue with grabbing the HTML text from a particular website using the embedded browser. I took your code and dropped in the following URL:
http://www.physionet.org/cgi-bin/atm/ATM
In the “Database” drop down under the “Input” cell, select any one of the databases. It will load the data for that particular study.
OBJECTIVE: Capture the text on the page for the time section of the study being viewed; the text in the blue bar just above the graphs (“Selected Input:”).
PROBLEM: HTML text field in browser object is not updated after navigating away from welcome page. Need to find a way to get the HTML code that is specific to the study data that is being displayed.
METHODS:
1) Used uiinspect.m (http://undocumentedmatlab.com/blog/uiinspect/) to find the displayed HTML for the data page of the study. Can’t find any field containing the new info.
2) Right clicked on the page and used the “Page Source” option. It shows the original welcome screen html
3) Right clicked on the page and used the “Refresh” option. This did not update the HTML text field of the browser object. It did keep you on the study page, and did not refresh to the welcome page, even though it had to resend the POSTDATA query.
4) Viewed the same page in Firefox and inspected the page source code. This gave me what I want; HTML code specific to the study.
Any help would be greatly appreciated. Thank you.
Here is the modified m-code from this post that has a link to the page that I am working with:
@thnxtay – I suggest that you load your webpage in some external browser that has more abilities to display the inner HTML code (e.g., Firebug on FireFox). Then you’d be able to see what goes on behind the scenes and try to use this info in the more-limited Matlab browser. If you would like further assistance on a consulting basis, please contact me via email.
Hi
I tried to use the code example above, to load an html file that I have created into the GUI, but I got the following message (inside the UI browser):
“This file exceeds the maximum size for the MATLAB Web Browser. It will open in your system browser.”
and indeed the html was opened with in internet explorer.
Any idea how to solve this problem?
Thanks
Kobbi
@Kobbi – try to minify your HTML