MATLAB Answers

GUI with live webcam preview not closing

6 views (last 30 days)
Hey everyone, I am trying to write a MATLAB GUI where I want to display a live output of the webcam on the interface alongside with a DAQ interface with a listener that sends current to coils. I followed several similar topics and now I have something that works fine. There remains a small problem though: I am unable to close the GUI window manually. The only thing that works is
close ALL FORCE
command. If I attempt to close GUI window normally, the command window outputs the warning:
Warning: Error occurred while executing the listener callback for event Custom defined for class asyncio.Channel:
Error using matlab.webcam.internal.PreviewController/closePreview
Invalid or deleted object.
> In asyncio.Channel/onCustomEvent (line 429)
In asyncio.Channel>@(source,data)obj.onCustomEvent(data.Type,data.Data) (line 361)
And the warning repeats each time I attempt to close. It appears that something is deleted before it should have been deleted but I don't understand what is happening. As a side note, the interface does not output such a warning if I remove the code related to live webcam preview.

  0 Comments

Sign in to comment.

Accepted Answer

Ganesh Regoti
Ganesh Regoti on 17 Jan 2020
Hi,
This is a known issue in closePreview function when you try to use custom GUI to preview the data.
This bug is fixed in R2019b. So, you can update your MATLAB to latest version.
The following code which defines its own figure CloseRequestFcn callback can be used as a workaround:
%% Connect to webcam
c = webcam(1);
%% Setup preview window
fig = figure('NumberTitle', 'off', 'MenuBar', 'none');
fig.Name = 'My Camera';
ax = axes(fig);
frame = snapshot(c);
im = image(ax, zeros(size(frame), 'uint8'));
axis(ax, 'image');
%% Start preview
preview(c, im)
setappdata(fig, 'cam', c);
fig.CloseRequestFcn = @closePreviewWindow_Callback;
%% Local functions
function closePreviewWindow_Callback(obj, ~)
c = getappdata(obj, 'cam');
closePreview(c)
delete(obj)
end
Hope this helps!

  1 Comment

Hakan Caldag
Hakan Caldag on 20 Jan 2020
I have just tried it in Matlab R2019b and I had no errors as you said. Thank you so much.

Sign in to comment.

More Answers (0)

Sign in to answer this question.

Products


Release

R2017b