title: in App Designer: preview(vid, himage) on gentl camera causes massive video lag and memory consumtion vs preview(vid)
body: <p>I'm using the MATLAB App Designer to create a program that has a video previewing functionality.</p>
  <p>I am displaying the video on a UIAxes object after creating an image handle with <code>image</code>.</p>
  <pre class="lang-matlab prettyprint-override"><code>app.feed = videoinput(&quot;gentl&quot;, 1 , 'Mono8');
  app.canvas = image(app.UIAxes, zeros(vidRes(2), vidRes(1), nBands) );
  frame = getsnapshot(app.feed);
  app.canvas.CData = frame;
  axis(app.UIAxes, 'image');
  preview(app.feed, app.canvas);
  </code></pre>
  <p>When this runs, the memory consumtion gets really high and the displayed video is delayed by 5 seconds or more.
  If i call <code>preview(app.feed)</code> for it to open in a separate window, this doesn't happen.</p>
  <p>Doing this in normal MATLAB doesn't cause any issues either.</p>
  <pre class="lang-matlab prettyprint-override"><code>figure
  axes
  vid = videoinput(&quot;gentl&quot;, 1, &quot;Mono8&quot;);
  frame = getsnapshot(vid);
  canvas = image(zeros(size(frame)));
  axis(&quot;image&quot;);
  preview(vid, canvas);
  </code></pre>
  <p>I tried using a timer to display single frames, but that was really slow.</p>
  <p>Any help is really appreciated.</p>
  <p>Here is the complete program, the custom <code>preview_fcn</code> is not the cause of the issue:</p>
  <pre class="lang-matlab prettyprint-override"><code>classdef app1 &lt; matlab.apps.AppBase
  
      % Properties that correspond to app components
      properties (Access = public)
          UIFigure                    matlab.ui.Figure
          Menu                        matlab.ui.container.Menu
          Menu2                       matlab.ui.container.Menu
          GridLayout                  matlab.ui.container.GridLayout
          LeftPanel                   matlab.ui.container.Panel
          GridLayout2                 matlab.ui.container.GridLayout
          Switch                      matlab.ui.control.Switch
          ResetCameraInterfaceButton  matlab.ui.control.Button
          PreviewPanel                matlab.ui.container.Panel
          GridLayout4                 matlab.ui.container.GridLayout
          StartButton                 matlab.ui.control.Button
          StopButton                  matlab.ui.control.Button
          RightPanel                  matlab.ui.container.Panel
          UIAxes                      matlab.ui.control.UIAxes
          Timestamp                   matlab.ui.control.Label
      end
  
      % Properties that correspond to apps with auto-reflow
      properties (Access = private)
          onePanelWidth = 576;
      end
  
      properties (Access = private)
          feed; % camera feed
          src;
          canvas;
          captureTimer;
      end  
      methods (Access = private)
      
          function preview_fcn(app, ~, event, himage)
              % Example update preview window function.
  
              % Get timestamp for frame.
              tstampstr = event.Timestamp;
              framerate = event.FrameRate;
              %status = event.Status;
              resolution = event.Resolution;
              
              % Set the value of the text label.
              app.Timestamp.Text = strcat(tstampstr, &quot; | &quot; , num2str(framerate), &quot; | &quot;, resolution);
              %app.Status.Text = strcat( &quot;| &quot;, status, &quot; | :&quot;, resolution);
              % Display image data.
              himage.CData = event.Data;
          end
          
          function reset_camera(app)
              app.ResetCameraInterfaceButton.Enable = false;
              app.canvas = image(app.UIAxes, zeros(1542, 2064, 3) );
              axis(app.UIAxes, 'image');
              imaqreset
              try
                  app.feed = videoinput(&quot;gentl&quot;, 1 , 'Mono8');
              catch
                  warndlg(&quot;Can't open Camera feed. Fix and reset interface!&quot;, &quot;Warning!&quot;)
                  return
              end
              %Settings for camera
              triggerconfig(app.feed, 'manual');
              app.feed.FramesPerTrigger = 1;
              app.src = getselectedsource(app.feed);
              app.src.ReverseY = 'True';
              %app.src.ExposureTime = 40000;
              
              % Setup preview container
              frame = getsnapshot(app.feed);
              vidRes = app.feed.VideoResolution;
              nBands = app.feed.NumberOfBands;
              app.canvas = image(app.UIAxes, zeros(vidRes(2), vidRes(1), nBands));
              colormap(app.UIAxes, gray);
              app.canvas.CData = frame;
              axis(app.UIAxes, 'image');
              
              setappdata(app.canvas, 'UpdatePreviewWindowFcn', @app.preview_fcn);
              
              app.StartButton.Enable = true;
              app.ResetCameraInterfaceButton.Enable = true;
          end
      end
  
      % Callbacks that handle component events
      methods (Access = private)
  
          % Code that executes after component creation
          function startupFcn(app)
              app.UIAxes.Title.String = '';
              app.UIAxes.XLabel.String = '';
              app.UIAxes.YLabel.String = '';
              app.UIAxes.Visible = 'off';
              app.UIAxes.XTick = [];
              app.UIAxes.YTick = [];
              app.reset_camera;
              
  %             app.captureTimer = timer(&quot;BusyMode&quot;, &quot;drop&quot;, &quot;ExecutionMode&quot;, &quot;fixedRate&quot;, &quot;Period&quot;, 0.01);
  %             app.captureTimer.StartFcn = @(~,~)app.setupCamera;
  %             app.captureTimer.TimerFcn = @(~,~)app.captureFrame;
                    
              %app.MeasureAngleButton.Enable = true;
          end
  
          % Changes arrangement of the app based on UIFigure width
          function updateAppLayout(app, event)
              currentFigureWidth = app.UIFigure.Position(3);
              if(currentFigureWidth &lt;= app.onePanelWidth)
                  % Change to a 2x1 grid
                  app.GridLayout.RowHeight = {480, 480};
                  app.GridLayout.ColumnWidth = {'1x'};
                  app.RightPanel.Layout.Row = 2;
                  app.RightPanel.Layout.Column = 1;
              else
                  % Change to a 1x2 grid
                  app.GridLayout.RowHeight = {'1x'};
                  app.GridLayout.ColumnWidth = {220, '1x'};
                  app.RightPanel.Layout.Row = 1;
                  app.RightPanel.Layout.Column = 2;
              end
          end
  
          % Button pushed function: StartButton
          function StartButtonPushed(app, event)
              preview(app.feed, app.canvas);
              app.StopButton.Enable = true;
              app.StartButton.Enable = false;
              %start(app.captureTimer);
              %app.src.ExposureAuto = 'Continuous';
          end
  
          % Close request function: UIFigure
          function UIFigureCloseRequest(app, event)
              try
                  stoppreview(app.feed);
                  delete(app.feed);
              catch
              end
              delete(app)
          end
  
          % Button pushed function: StopButton
          function StopButtonPushed(app, event)
              stoppreview(app.feed);
              app.StopButton.Enable = false;
              app.StartButton.Enable = true;
              %stop(app.captureTimer);
          end
  
          % Button pushed function: ResetCameraInterfaceButton
          function ResetCameraInterfaceButtonPushed(app, event)
              app.reset_camera;
          end
      end
  
      % Component initialization
      methods (Access = private)
  
          % Create UIFigure and components
          function createComponents(app)
  
              % Create UIFigure and hide until all components are created
              app.UIFigure = uifigure('Visible', 'off');
              app.UIFigure.AutoResizeChildren = 'off';
              app.UIFigure.Position = [100 100 640 480];
              app.UIFigure.Name = 'MATLAB App';
              app.UIFigure.CloseRequestFcn = createCallbackFcn(app, @UIFigureCloseRequest, true);
              app.UIFigure.SizeChangedFcn = createCallbackFcn(app, @updateAppLayout, true);
              app.UIFigure.KeyPressFcn = createCallbackFcn(app, @UIFigureKeyPress, true);
  
              % Create Menu
              app.Menu = uimenu(app.UIFigure);
              app.Menu.Text = 'Menu';
  
              % Create Menu2
              app.Menu2 = uimenu(app.UIFigure);
              app.Menu2.Text = 'Menu2';
  
              % Create GridLayout
              app.GridLayout = uigridlayout(app.UIFigure);
              app.GridLayout.ColumnWidth = {220, '1x'};
              app.GridLayout.RowHeight = {'1x'};
              app.GridLayout.ColumnSpacing = 0;
              app.GridLayout.RowSpacing = 0;
              app.GridLayout.Padding = [0 0 0 0];
              app.GridLayout.Scrollable = 'on';
  
              % Create LeftPanel
              app.LeftPanel = uipanel(app.GridLayout);
              app.LeftPanel.Layout.Row = 1;
              app.LeftPanel.Layout.Column = 1;
  
              % Create GridLayout2
              app.GridLayout2 = uigridlayout(app.LeftPanel);
              app.GridLayout2.RowHeight = {'1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x'};
              app.GridLayout2.RowSpacing = 25;
  
              % Create Switch
              app.Switch = uiswitch(app.GridLayout2, 'slider');
              app.Switch.Layout.Row = 8;
              app.Switch.Layout.Column = 2;
  
              % Create Label
              app.Label = uilabel(app.GridLayout2);
              app.Label.Layout.Row = 7;
              app.Label.Layout.Column = 2;
  
              % Create ResetCameraInterfaceButton
              app.ResetCameraInterfaceButton = uibutton(app.GridLayout2, 'push');
              app.ResetCameraInterfaceButton.ButtonPushedFcn = createCallbackFcn(app, @ResetCameraInterfaceButtonPushed, true);
              app.ResetCameraInterfaceButton.Enable = 'off';
              app.ResetCameraInterfaceButton.Layout.Row = 6;
              app.ResetCameraInterfaceButton.Layout.Column = 1;
              app.ResetCameraInterfaceButton.Text = {'Reset Camera'; 'Interface'};
  
              % Create GridLayout3
              app.GridLayout3 = uigridlayout(app.ROIPanel);
              app.GridLayout3.ColumnWidth = {'1x'};
              app.GridLayout3.RowHeight = {'1x', '1x', '1x'};
  
              % Create PreviewPanel
              app.PreviewPanel = uipanel(app.GridLayout2);
              app.PreviewPanel.Title = 'Preview';
              app.PreviewPanel.Layout.Row = [1 3];
              app.PreviewPanel.Layout.Column = 1;
  
              % Create GridLayout4
              app.GridLayout4 = uigridlayout(app.PreviewPanel);
              app.GridLayout4.ColumnWidth = {'1x'};
  
              % Create StartButton
              app.StartButton = uibutton(app.GridLayout4, 'push');
              app.StartButton.ButtonPushedFcn = createCallbackFcn(app, @StartButtonPushed, true);
              app.StartButton.Enable = 'off';
              app.StartButton.Layout.Row = 1;
              app.StartButton.Layout.Column = 1;
              app.StartButton.Text = 'Start';
  
              % Create StopButton
              app.StopButton = uibutton(app.GridLayout4, 'push');
              app.StopButton.ButtonPushedFcn = createCallbackFcn(app, @StopButtonPushed, true);
              app.StopButton.Enable = 'off';
              app.StopButton.Layout.Row = 2;
              app.StopButton.Layout.Column = 1;
              app.StopButton.Text = 'Stop';
  
              % Create RightPanel
              app.RightPanel = uipanel(app.GridLayout);
              app.RightPanel.Layout.Row = 1;
              app.RightPanel.Layout.Column = 2;
  
              % Create UIAxes
              app.UIAxes = uiaxes(app.RightPanel);
              title(app.UIAxes, 'Title')
              xlabel(app.UIAxes, 'X')
              ylabel(app.UIAxes, 'Y')
              app.UIAxes.Position = [6 28 408 425];
  
              % Create Timestamp
              app.Timestamp = uilabel(app.RightPanel);
              app.Timestamp.Position = [6 1 408 22];
              app.Timestamp.Text = '';
  
              % Show the figure after all components are created
              app.UIFigure.Visible = 'on';
          end
      end
  
      % App creation and deletion
      methods (Access = public)
  
          % Construct app
          function app = app1
  
              % Create UIFigure and components
              createComponents(app)
  
              % Register the app with App Designer
              registerApp(app, app.UIFigure)
  
              % Execute the startup function
              runStartupFcn(app, @startupFcn)
  
              if nargout == 0
                  clear app
              end
          end
  
          % Code that executes before app deletion
          function delete(app)
  
              % Delete UIFigure when app is deleted
              delete(app.UIFigure)
          end
      end
  end
  </code></pre>