How can I play video in MATLAB GUI after browsing the video?

조회 수: 2 (최근 30일)
Isha Pandya
Isha Pandya 2016년 9월 25일
댓글: Walter Roberson 2020년 1월 4일
I have created the following GUI in MATLAB. I want to browse the video and display the path of video in the edit textbox besides the browse button. And after clicking the "Play" button, it should play the browsed video in the static text box. Please help me with the code. Following is the GUI:

답변 (7개)

Walter Roberson
Walter Roberson 2016년 9월 25일
function browse_Callback(hObject, event, handles)
[chosenfile, chosenpath] = uigetfile('*.avi', 'Select a video');
if ~ischar(chosenfile)
return; %user canceled dialog
end
filename = fullfile(chosenpath, chosenfile);
set(handles.edit1, filename);
function play_Callback(hobject, event, handles)
filename = get(handles.edit1, filename);
if ~exist(filename, 'file')
warndlg( 'Text in edit box is not the name of a file');
return
end
try
obj = VideoReader(filename);
catch
warndlg( 'File named in edit box does not appear to be a usable movie file');
return
end
ax = handles.ax1;
while hasFrame(obj)
vidFrame = readFrame(obj);
image(vidFrame, 'Parent', ax);
set(ax, 'Visible', 'off');
pause(1/obj.FrameRate);
end
clear obj
Notice, though, that this code displays in an axes, not in a static text. A static text box cannot hold graphics. If there was some deep reason why you needed the video displayed on a uicontrol then you would use a uicontrol('style', 'push', 'enable', 'off') and update its Cdata property with the contents of the frame -- but that would seldom be better than displaying it in an axes.
I would also suggest to you that it would be better if considered making that edit box into a static text box. When you have it as an edit box then the user might edit the string, which is why it is necessary to validate that the string still designates a file. If you make it a static text box then the only way for the user to put something in there is by the Browse button, so you could be sure that it was a file named.
You should be careful, though: when you allow the user to browse to arbitrary locations, the complete pathname to the file (which is what you write in the edit box) might not fit within the space you allocate. Edit boxes do at least allow the user to position within the text if they want to see it all, but the default is at the beginning, which is the directory part rather than the file name part. Static text boxes attempt to "wrap" the text to multiple lines if the box is not wide enough for all if it; that is not going to be very successful if the name is too long for the area you give.
  댓글 수: 2
Isha Pandya
Isha Pandya 2016년 9월 25일
I have made slight changes in GUI as suggested by you. Instead of static text I am using axes. But I am getting the following errors:
Error using hg.uicontrol/set The name 'D:\Video3.wmv' is not an accessible property for an instance of class 'uicontrol'.
Error in play2>pushbutton1_Callback (line 85) set(handles.edit1, filename);
Error in gui_mainfcn (line 96) feval(varargin{:});
Error in play2 (line 42) gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)play2('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
I am new to MATLAB and need some explanation regarding these errors. How to solve these errors?
Walter Roberson
Walter Roberson 2016년 9월 25일
Sorry, it should have been
set(handles.edit1, 'String', filename);
and
filename = get(handles.edit1, 'String');

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2016년 9월 25일
Description
implay opens the Video Viewer app. You can use Video Viewer to show MATLAB® movies, videos, or image sequences (also called image stacks). To select the movie or image sequence that you want to play,................
  댓글 수: 1
Walter Roberson
Walter Roberson 2016년 9월 25일
implay() opens its own figure to play in; you cannot use implay() to play within a specific area of your GUI.

댓글을 달려면 로그인하십시오.


Pavel Chatterjee
Pavel Chatterjee 2018년 4월 15일
I am getting the following error
Reference to non-existent field 'edit1'. Error in untitled10>pushbutton1_Callback (line 85) filename = get(handles.edit1, 'String'); Error in gui_mainfcn (line 95) feval(varargin{:}); Error in untitled10 (line 42) gui_mainfcn(gui_State, varargin{:}); Error in matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)untitled10('pushbutton1_Callback',hObject,eventdata,guidata(hObject)) Error while evaluating UIControl Callback.
  댓글 수: 1
Walter Roberson
Walter Roberson 2018년 4월 16일
You do not have any graphic object with tag 'edit1'. You should change the edit1 to the tag of your edit box.

댓글을 달려면 로그인하십시오.


Sevthia Nugraha
Sevthia Nugraha 2018년 6월 22일
Undefined function 'hasFrame' for input arguments of type 'VideoReader'.
Error in SIMULASI_CROSSTALK>pushbutton7_Callback (line 983) while hasFrame(obj)
Error in gui_mainfcn (line 95) feval(varargin{:});
Error in SIMULASI_CROSSTALK (line 42) gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)SIMULASI_CROSSTALK('pushbutton7_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
i'm still getting these error, so what should i do??
  댓글 수: 3
Image Analyst
Image Analyst 2018년 6월 23일
"so what should i do??" My answer is "upgrade to the latest MATLAB version."
Sevthia Nugraha
Sevthia Nugraha 2018년 7월 15일
that's helping me a lot, thank you

댓글을 달려면 로그인하십시오.


prasetya zulfikar
prasetya zulfikar 2019년 4월 18일
thx walter robenson its work !!!!!!!!!!

Salma Fatia
Salma Fatia 2019년 11월 16일
The code is also working with me, thank u so much Walter Roberson

Mazhar ali Khan
Mazhar ali Khan 2020년 1월 4일
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)GUI('browse_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
i m facing this error
  댓글 수: 1
Walter Roberson
Walter Roberson 2020년 1월 4일
At the command line give the command
dbstop if error
And then run the code. When it stops on the error, give the command
dbstack
And show us the complete results. Also show us size() and class() of every variable mentioned on the line that it stopped on. And tell us which MATLAB version you are using.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by