View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
setFigTransparency - set figure transparency / fading

5.0 | 6 ratings Rate this file 8 Downloads (last 30 days) File Size: 3.82 KB File ID: #30583 Version: 1.1
image thumbnail

setFigTransparency - set figure transparency / fading


Yair Altman (view profile)


01 Mar 2011 (Updated )

setFigTransparency sets the transparency/opacity of a figure window, with optional fading effect

| Watch this File

File Information


   oldAlpha = setFigTransparency(hFig, alpha, fadeDuration, blockingFlag)

   setFigTransparency sets the figure hFig's transparency value. The entire figure window, including all internal menus, toolbars and components, is made transparent according to the alpha value.
   oldAlpha = setFigTransparency(...) returns the old transparency value of the specified figure, prior to its modification.

   This submission is based on an original idea implemented by Malcolm Lidierth in his MUtilities submission:

Input parameters: (all parameters are optional)
     hFig (default=gcf) - Handle(s) of the modified figure(s). If component handle(s) is/are specified, the containing figure(s) will be inferred and used.
     alpha (default=0.5) - Transparency value, between 0.0 (=fully transparent) and 1.0 (=fully opaque). Note that all Matlab figure windows are created opaque. alpha<0 indicates that alpha value should not be modified.
     fadeDuration (default=0) - Number of seconds for fade-in/fade-out effect. Note: default value of 0 means immediately (no fading)
     blockingFlag - (default=true) Whether or not the function should wait for the fade-in/fade-out effect to complete before returning

   oldAlpha = setFigTransparency(hFig,-1); % get hFig's current alpha
   oldAlpha = setFigTransparency(hFig); % set hFig's alpha to 0.5 (semi-transparent)
   oldAlpha = setFigTransparency(hFig,0.7); % set hFig's alpha to 0.7
   oldAlpha = setFigTransparency([hFig1,hFig2],0.7); % set transparency for several figures
   oldAlpha = setFigTransparency(hFig,0.3,1.5,false); % non-blocking fade over 1.5 secs

Bugs and suggestions:
   Please send to Yair Altman (altmany at gmail dot com)

   This code heavily relies on undocumented and unsupported Matlab functionality. It works on Matlab 7.9 (R2009b) and higher, but use at your own risk!


Get J Frame Retrieves A Figure's Underlying Java Frame and Enable/Disable Entire Figure Window inspired this file.

This file inspired Blur Figure Blurs And Prevents Interaction On A Figure Window, Undecorate Fig Remove/Restore Figure Border And Title Bar, and Window Api.

MATLAB release MATLAB 7.11 (R2010b)
Other requirements Matlab 7.9 (R2009b) and higher, or a retrofit of JRE 1.6.0_10 and higher in any previous Matlab release.
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (13)
27 Jun 2013 Yair Altman

Yair Altman (view profile)

IMPORTANT NOTE: this utility DOES NOT WORK on R2013b/Java7 and higher, only on earlier releases. The failure is due to a change in the way that transparency works in Java 7 compared to earlier releases. If I ever find a solution, I will post it here. In the meantime, please do not comment about this issue here.

Comment only
23 Jun 2013 AMB

AMB (view profile)

I think this function is fantastic and I want to keep it useful, I am on a MAC and Using Matlab (R2013a), the line

figure(2);plot([0 0],[1 1]);shg;setFigTransparency(gcf)

works fine; however Using Matlab (R2013b), with Java 1.7.0_11-b21 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode, the same line produces an error

figure(2);plot([0 0],[1 1]);shg;setFigTransparency(gcf)
Error using setFigTransparency (line 137)
Java exception occurred:
java.awt.IllegalComponentStateException: The frame is decorated
at java.awt.Frame.setOpacity(
at java.awt.Window$1.setOpacity(
at com.sun.awt.AWTUtilities.setWindowOpacity(

which prevents the figure from becoming transparent.

Below is some standalone code which when pasted in the commandwindow will quickly show the problem

figure(2);set(gcf,'color',[1 0 0]);
plot([0 0],[1 1]);

[lastWarnMsg lastWarnId] = lastwarn;
warning(oldJFWarning.state, 'MATLAB:HandleGraphics:ObsoletedProperty:JavaFrame');
lastwarn(lastWarnMsg, lastWarnId);
jWindow = jFigPeer.fHG1Client.getWindow;
newAlpha = .5;

% throws the following exception in (R2013b) and does not in (R2013a)
% Java exception occurred:
% java.awt.IllegalComponentStateException: The frame is decorated
% at java.awt.Frame.setOpacity(
% at java.awt.Window$1.setOpacity(
% at com.sun.awt.AWTUtilities.setWindowOpacity(


Are there any workarounds?

Comment only
17 Oct 2012 Alex

Alex (view profile)


Comment only
17 Oct 2012 Alex

Alex (view profile)


21 Sep 2012 Yair Altman

Yair Altman (view profile)

@Razvan - setFigTansparency cannot work on an invisible figure for technical reasons. However, you can create a *Visible* figure with initial position outside the screen boundaries, which is effectively hidden. This works nicely (at least for non-docked figures):

hFig = figure('Visible', 'off');
pos = get(hFig,'Position');
set(hFig, 'Position',[-100,-100,1,1], 'Visible','on');
setFigTransparency(hFig, 0);
set(hFig, 'Position',pos);
setFigTransparency(hFig, 1,1);

Comment only
20 Sep 2012 Razvan

Razvan (view profile)

This function is very nice!

I couldn't figure out though how I can fade in a figure. I tried to set the 'visible' property to 'off' first then set alpha to 0, but this doesn't work.

The only option that I found is something like this:

hFig = figure('Visible', 'off');
set(hFig, 'Visible', 'on')
setFigTransparency(hFig, 0);
setFigTransparency(hFig, 1, 1);

but it doesn't look nice because the figure pops up before it disappears and fades in again...
Any advise?


Comment only
20 Sep 2012 Razvan

Razvan (view profile)

16 Oct 2011 Jveer

Jveer (view profile)

Thank you for the fix.

13 Oct 2011 Yair Altman

Yair Altman (view profile)

@Jveer - I uploaded a fix for R2011b. It should be available here shortly. Note that your method of attracting my attention is entirely unprofessional.

Comment only
26 Sep 2011 Jveer

Jveer (view profile)


This doesn't work with R2011b

I get the following error:

Error using setFigTransparency>getJFrame (line 194)
Cannot retrieve the Java Frame reference for figure 174.0021

Error in setFigTransparency (line 105)
jff = getJFrame(hFigCell{figIdx});

Error in MPMSimPre>TF_Callback (line 1536)

So I re-rate.

Please advise.

13 May 2011 Jan Simon

Jan Simon (view profile)

After I installed JRE 1.6.0_24, the UITREE objects of Matlab 2009a cannot be reliably resized by "set(TreeH, 'Position', Pos)": Sometimes it works, sometimes the update appears after some seconds only, or not at all. I'm absolutely sure, that Yair could find a solution, but this is actually not his job. It would be nice if TMW offers a long-term supported MATLAB release which accepts updated JREs seamingless.
Now I'm using the Windows-API to enable alpha-blending. This has the additional feature, that I can create a full size window (not just maximized, but all window borders outside the screen) and a stencil color to let the background disappear. This allows to create plots transparently "in" the command window. Ugly. Does this work with the Java methods also?
I rate this with 5 stars, but I will not use it with a retrofit JRE.

09 Mar 2011 Yair Altman

Yair Altman (view profile)

Note: a retrofit of JRE means installing JRE 1.6.0_10 (also called "Java 6 update 10"), or any later version (e.g., the latest available version today is 1.6 update 24). The JRE can be downloaded from and you can configure Matlab to use it according to the instructions in

All of this is only necessary in Matlab releases R2009a (7.8) and earlier, which pre-bundle an earlier JRE version that does not support window frame transparency/opacity used in this submission.

Matlab R2009b (7.9) and onward, at least on Microsoft Windows, pre-bundle a JRE version that does support transparency/opacity and so do not require a retrofit.

Comment only
04 Mar 2011 Jveer

Jveer (view profile)


13 Oct 2011 1.1

Fix for R2011b

Contact us