title: I want to control the transparency of patches using a slider that are being written on an image in another set of figures in Matlab?
body: <p>I have a figure with a slider on it and then I want to have an external figure that displays in image. I want to be able to use the slider to change the transparency patches I am drawing on the image. </p>
  
  <p>Currently this works but it generates a new image every time I move the slider. I would like the patch to be updated on the current image instead of opening a new window. </p>
  
  <p>I have been having a difficult time figuring this out. I think I need to define the image figure and handles outside of the slider callback, but I cant seem to figure it out. I have some of my code below. </p>
  
  <p>I have updated my code to include your suggestions. However I am getting this error</p>
  
  <pre><code>??? Error using ==&gt; set
  Invalid property found.
  Object Name :  root
  Property Name :  'FaceAlpha'.
  
  Error in ==&gt; readraw4&gt;slider_callback at 539
          set(hpatch, 'FaceAlpha',alpha)
  </code></pre>
  
  <p>I am not sure how to fix this or what is going wrong. Everything else seems to be working though.</p>
  
  <pre><code>hFig = figure('menubar','none');
  hAx = axes('Parent',hFig);
  
  hSlider = uicontrol('Parent',hFig, 'Style','slider', ...
      'Value',0.5, 'Min',0, 'Max',1, 'SliderStep',[1 10]./100, ...
      'Units','pixels', 'Position',[10 10 200 20], ...
      'Callback',@slider_callback);
  hTxt = uicontrol('Parent',hFig, 'Style','text', 'String','0.5', ...
      'Units','pixels', 'Position',[290 28 30 15]);
  
  imshow(im,'DisplayRange', [0.05 0.4], 'Parent',hAx)
  
  hpatch = [];
  
  for i =1:200
  
      if spectImage(i) == 1
          %disp('hi');
          hpatch(i) = patch([x2(i+18) x2(i+19) x2(i+2) x2(i+1)],[y2(i+18) y2(i+19) y2(i+2) y2(i+1)],[0 0 0 0],'Parent',hAx, 'FaceColor','r', 'EdgeColor','none', 'FaceAlpha',0.5)
          else
              hpatch(i) = patch([x2(i+17) x2(i+18) x2(i+1) x2(i)],[y2(i+17) y2(i+18) y2(i+1) y2(i)],[0 0 0 0],'Parent',hAx, 'FaceColor','r', 'EdgeColor','none', 'FaceAlpha',0.5);
      end
  end
  
  
  function slider_callback(hObj,eventdata)
      %# get new alpha value
      alpha = get(hObj,'Value');
  
      %# update patches transparency and label
      set(hTxt, 'String',num2str(alpha,'%.02f'))
      set(hpatch, 'FaceAlpha',alpha)
  end
  </code></pre>