Viewing image displayed on axes - matlab

(http://s1273.photobucket.com/user/Chethan_tv/media/CBIR_zpsb48bce14.jpg.html)
Above image is my final output, open push button is used to view image displayed on respective axes.
I've used following code for displaying
function open1_Callback(filename, hObject, eventdata, handles)
% hObject handle to open1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
fid=fopen(filename);
ax = handles.(sprintf('axes%d', 6));
imshow(ax)
Where, 6 is axes number. But I'm getting error like
undefined variable handles
To display image on axes I've used following code
function displayResults(filename,hObject, eventdata, handles)
% Open 'filename' file... for reading...
fid = fopen(filename);
for N=6:1:10
imagename = fgetl(fid);
if ~ischar(imagename), break, end % Meaning: End of File...
[x,map]=imread(imagename);
rgb=ind2rgb(x,map);
ax = handles.(sprintf('axes%d', N));
image(rgb, 'Parent', ax);
set(ax, 'Visible','off');
%xlabel(imagename);
end
guidata(hObject,handles)
filename is a text file.
HOW TO DISPLAY RESPECTIVE IMAGE USING PUSHBUTTON?

This is because you messed the code generated by GUIDE.
Normally callback functione definition looks like this:
function SomeButton_Callback(hObject, eventdata, handles)
But in your code you write
function open1_Callback(filename, hObject, eventdata, handles)
But guide still sends three arguments to the callback function (hObject, eventdata, and handles) in this specific order. So MatLab gets confused and throws an error.
You better put your filename into handles structure in *_OpeningFcn function and then use it from there in all calbacks.
At the end of your *_OpeningFcn you should add the following:
% Here you may put all the data you need in your GUI
% just be sure to keep all the fields in handles structure from overwriting
% Safe way is to add MyData field and add all the stuff to it
handles.MyData.ListFileName = 'FileName.txt';
% the next two lienes are generated by GUIDE
% Update handles structure
guidata(hObject, handles);
Then in callback function of your button
function open1_Callback(hObject, eventdata, handles)
% hObject handle to open1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% open the file
fid=fopen(handles.MyData.ListFileName);
% load lines from the file
% and do what is needed
for N=6:1:10
imagename = fgetl(fid);
if ~ischar(imagename), break, end % Meaning: End of File...
[x,map]=imread(imagename);
rgb=ind2rgb(x,map);
ax = handles.(sprintf('axes%d', N));
image(rgb, 'Parent', ax);
set(ax, 'Visible','off');
%xlabel(imagename);
end
% dont' forget to close the file
fclose(fid);
% If your callback function modifies data in handles.MyData structure
% you MUST update it back otherwise subsequent call-backs will not see it
guidata(hObject, handles);
And yes, all the files you open with fopen function should be closed with fclose. You will learn it hard way when you'll not be able to update your file in your favorite editor because another programs is using it.
See also (Making universal variables in MATLAB GUI)
UPDATE to reflect discussion in comments:
To achieve the behavior you want I'd do the following:
At the end of your *_OpeningFcn add the following:
% Here you may put all the data you need in your GUI
% just be sure to keep all the fields in handles structure from overwriting
% Safe way is to add MyData field and add all the stuff to it
handles.MyData.ListFileName = 'FileName.txt';
handles.MyData.FileNames = {}; % here we store all the image names
handles.MyData.Images = {}; % here we store all images
% Now we parse data from the file
fid=fopen(handles.MyData.ListFileName);
for N=6:1:10
imagename = fgetl(fid);
if ~ischar(imagename), break, end % Meaning: End of File...
[x,map]=imread(imagename);
rgb=ind2rgb(x,map);
ax = handles.(sprintf('axes%d', N));
image(rgb, 'Parent', ax);
set(ax, 'Visible','off');
%xlabel(imagename);
% store file name and image itself for later use
handles.MyData.Images{N} = rgb;
handles.MyData.FileNames{N} = imagename;
% we have buttons like open1 open2 open3 etc...
% add some info to the open buttons
% so they will be aware which image they display
btn = handles.(sprintf('open%d', N));
set(btn, 'UserData',N);
end
% dont' forget to close the file
fclose(fid);
% the next two lienes are generated by GUIDE
% Update handles structure
guidata(hObject, handles);
Then in callback function for your open button do the following
function open1_Callback(hObject, eventdata, handles)
% hObject handle to open1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
N = get(hObject,'UserData');
rgb = handles.MyData.Images{N};
ax = handles.(sprintf('axes%d', N));
% create figure in 'modal' mode, so user have to close it to continue
figure('WindowStyle','modal', 'Name',handles.MyData.FileNames{N} );
% show image
image(rgb, 'Parent', ax);
set(ax, 'Visible','off');
% If your callback function modifies data in handles.MyData structure
% you MUST update it back otherwise subsequent call-backs will not see it
guidata(hObject, handles);
Basically, this callback is universal: it should work without modifications for all your open buttons. You may even change callback function of open2, open3 ... buttons in GUIDE to open1_Callback.

Related

Connecting MATLAB GUI to .m file

I have a m file, which chops the signal and applies filter according to the cut off frequency(Fc).
M file:
classdef Container < handle
properties
segments = struct('signal', {}, 'time', {},'ref',{}); %empty structure with correct fields
end
methods
function this = addsignal(this, signal, time,fc)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%chopping of the signals%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
interval = diff(time);
[~, locations] = findpeaks(interval,'THRESHOLD',0.7);
edges = [0; locations; numel(signal)+1];
newsegments = struct('signal', cell(numel(edges)-1, 1), 'time', cell(numel(edges)-1, 1));
%this loop works for no peaks, 1 peak and more than one peak (because of the 0 and numel+1)
for edgeidx = 1 : numel(edges) - 1
newsegments(edgeidx).signal = signal(edges(edgeidx)+1 : edges(edgeidx+1)-1);
newsegments(edgeidx).time = time(edges(edgeidx)+1 : edges(edgeidx+1)-1);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%filtering%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
f = ltiFilter.PT1(); % another class which has filters
f.Ts = mean(diff(time));
f.fc = fc; % i want to set this value from the slider%%%%%
f.zeroPhaseShift = 1;
for i = 1:length(newsegments)
newsegments(i).ref = f.eval(newsegments(i).signal,newsegments(i).signal(1)); % application of the filter.
newsegments(i).ref = newsegments(i).ref';
end
this.segments = [this.segments; newsegments];
end
end
end
I created a GUI which has a plot and a slider(for cut off frequcy) which is shown in code as f.fc
when i created the GUI, Matlab automatically created a Code for me(i must say, I din't understand that much)
GUI code:
function varargout = GUI(varargin)
% GUI MATLAB code for GUI.fig
% GUI, by itself, creates a new GUI or raises the existing
% singleton*.
%
% H = GUI returns the handle to a new GUI or the handle to
% the existing singleton*.
%
% GUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in GUI.M with the given input arguments.
%
% GUI('Property','Value',...) creates a new GUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before GUI_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to GUI_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help GUI
% Last Modified by GUIDE v2.5 15-Jul-2016 09:37:09
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #GUI_OpeningFcn, ...
'gui_OutputFcn', #GUI_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before GUI is made visible.
function GUI_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to GUI (see VARARGIN)
% Choose default command line output for GUI
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes GUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = GUI_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject handle to slider1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
% hObject handle to slider1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
What i want to do is, i want to connect the GUI to my m script, when user sildes the slider . it should show the change in graph automatically and when he clicks on apply. the value of slider should be taken and should be available in my m file.
Any leads will be helpful.
From code sample pasted, I'm assuming you are using MATLAB GUIDE. Let's assume name of slider control is "slider1".
Add a callback function for "slider" using GUIDE.
It will create a function "function slider1_Callback(hObject, eventdata, handles)" in your code.
Now, to get value selected from slider movement use "get" function with "hObject".
E.g. SliderVal=get(hObject,'Value');
Now if you want to know value of slider selection from other callbacks (such as Apply button)
use handles structure.
E.g.: SliderVal=get(handles.slider1,'Value');
Based on slider value, received you need to re-draw plot area.
I hope this helps as clue you are expecting.
Edit1:
For followup comment, how to get data from other M-file:
This will be very tricky. Because, you need to know handle of slider control in other M-file.
One of the ways would be to get handle of figure first.
Set "HandleVisibility" property of GUI figure (via GUIDE) to "ON".
Call "figures = get(0,'Children');" from M-script to get list of all open figures. This will give vector of figure handles.
Scan through list of children and get handle of your application. (This can be done via using property get(figures(1),'Name')).
Let's assume you found that handle, repeat same process again to get children from it. get(figHandle,'Children').
Scan through children and find slider control handle similar to approach as in step 3.
Now you have access to control and it's data.
I hope you understood it.

Create a gui in matlab

I want to create a gui in matlab with a push button and a panel.
Requirements: Upon clicking the button the dialogue must open to choose a file and after selecting image from this dialogue the image must be shown to that panel of gui.
How do I do this?
How to show the image on the gui panel?
My current status so far:
function pushbutton1_Callback(hObject, eventdata, handles)
[filename, pathname] = ...
uigetfile({'*.jpg';'*.png';'*.jpeg';'*.bmp';'*.*'},'File Selector');
set(handles.axes2, 'Visible','on');
imshow(IMG,'Parent',TheAxisHandleToDrawOn)
if isequal(filename,0)
disp('Image upload Canceled')
end
I tried this but it's not working, how to do this?
With respect to your callback you have first to read the selected image by using imread; you can use fullfile to build the complete filename.
Then you use the matrix returned by imread as input for the imshow function.
Edit following the comments
In order to show the image within a uipanel you have to add an axes in the panel and use it as parent in the call to imshow.
If the image you want to display is not in the current folder or in a folder on the MATLAB path, you have to specify the full path name (ref. imread).
In the following you can find the .m file of the GUI I've build for test.
The GUI contains:
an axes (tag: axes2)
a uipanel (tag: uipanel1)
an axes (tag: axes_up) embedded in the uipanel
two radiobuttons (tag: radiobutton1 and radiobutton2) to select the axes on which to show the image
two statictext fields (tag: text2 and text3) to show the filename of the selected images
a pushbutton (tag: pushbutton1) to select the image to be displayed
a pushbutton (tag: pushbutton3) to run the imcontrast tool in case of selection of a .dcm image
The code has been tested on R2012b and 2015b.
function varargout = disp_fig(varargin)
% DISP_FIG MATLAB code for disp_fig.fig
% DISP_FIG, by itself, creates a new DISP_FIG or raises the existing
% singleton*.
%
% H = DISP_FIG returns the handle to a new DISP_FIG or the handle to
% the existing singleton*.
%
% DISP_FIG('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in DISP_FIG.M with the given input arguments.
%
% DISP_FIG('Property','Value',...) creates a new DISP_FIG or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before disp_fig_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to disp_fig_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help disp_fig
% Last Modified by GUIDE v2.5 25-Apr-2016 14:18:18
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #disp_fig_OpeningFcn, ...
'gui_OutputFcn', #disp_fig_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before disp_fig is made visible.
function disp_fig_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to disp_fig (see VARARGIN)
% Choose default command line output for disp_fig
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes disp_fig wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = disp_fig_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Select the image to be displayed
[filename, pathname] = ...
uigetfile({'*.jpg';'*.png';'*.jpeg';'*.bmp';'*.*'},'File Selector');
% Build the full filename
img_file=fullfile(pathname,filename)
% Split the filename to get its extension
[the_path,the_name,the_ext]=fileparts(img_file);
% Set the current StaticText uicontrol based on the selected RadioButton
curr_txt=handles.text2;
if(get(handles.radiobutton2,'value'))
curr_txt=handles.text3;
end
% Check for image valid selection
if isequal(filename,0)
disp('Image upload Canceled')
set(curr_txt,'string','Image upload Canceled')
else
% If an image has been selected
set(curr_txt,'string',img_file)
% If the image is a ".dcm"
if(strcmp(the_ext,'.dcm'))
% Read it with dicomread and enable the pushbutton to run the
% IMCONTRAST tool
IMG=dicomread(img_file);
set(handles.pushbutton3,'visible','on')
else
% If the image is a ".jpg", ".bmp", ...
% Read it with imread and disable the pushbutton to run the
% IMCONTRAST tool
IMG=imread(img_file);
set(handles.pushbutton3,'visible','off')
end
% Identify the axes on which to display the image according to the
% selected radiobutton
if(get(handles.radiobutton1,'value'))
the_parent=handles.axes2;
set(handles.axes2, 'Visible','on');
else
the_parent=handles.axes_up;
end
% Showthe image
imshow(IMG,'Parent',the_parent)
end
% --- Executes on button press in radiobutton1.
function radiobutton1_Callback(hObject, eventdata, handles)
% hObject handle to radiobutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of radiobutton1
% If the first radiobutton has been selected, disable the second onre
r1=get(handles.radiobutton1,'value');
if(r1)
set(handles.radiobutton2,'value',0);
end
% --- Executes on button press in radiobutton2.
function radiobutton2_Callback(hObject, eventdata, handles)
% hObject handle to radiobutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of radiobutton2
% If the second radiobutton has been selected, disable the first one
r2=get(handles.radiobutton2,'value');
if(r2)
set(handles.radiobutton1,'value',0);
end
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Run the IMCONTRAST tool to the proper axes according to the selected
% radiobutton
r1=get(handles.radiobutton1,'value');
if(r1)
imcontrast(handles.axes2)
else
imcontrast(handles.axes_up)
end
Structure of the GUI
The GUI ... working
To test the .dcm image display I've used, as examples, the images from DICOM sample image sets.
Hope this helps.
Qapla'
this is the final solution for the problem
function browse_Callback(hObject, eventdata, handles)
% hObject handle to browse (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[filename, pathname]=uigetfile( {'.jpg';'.gif';'.png';'.bmp'},'Select file');
MyImage = strcat(pathname, filename);
%This code checks if the user pressed cancel on the dialog.
if isequal(filename,0) || isequal(pathname,0)
uiwait(msgbox ('User pressed cancel','failed','modal') )
hold on;
else
uiwait(msgbox('User selected image sucessfully','sucess','modal'));
hold off;
imshow(MyImage,'Parent',handles.axes1);
end
global Imagevariable;
Imagevariable=MyImage;
handles.output = hObject;
guidata(hObject, handles);

display a screen/frame to display dicom image in matlab

I have a matlab program to upload a folder of dicom images. I want to display a black screen/frame where in the image would be loaded. Now, the image is displayed over the browse button.
Is there a way to do it ?
Here's my code:
function varargout = ui(varargin)
% UI MATLAB code for ui.fig
% UI, by itself, creates a new UI or raises the existing
% singleton*.
%
% H = UI returns the handle to a new UI or the handle to
% the existing singleton*.
%
% UI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in UI.M with the given input arguments.
%
% UI('Property','Value',...) creates a new UI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before ui_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to ui_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help ui
% Last Modified by GUIDE v2.5 17-Nov-2015 13:11:51
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #ui_OpeningFcn, ...
'gui_OutputFcn', #ui_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
end
% --- Executes just before ui is made visible.
function ui_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to ui (see VARARGIN)
% Choose default command line output for ui
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes ui wait for user response (see UIRESUME)
% uiwait(handles.figure1);
end
% --- Outputs from this function are returned to the command line.
function varargout = ui_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
end
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global files;
global dname;
dname = uigetdir('Select the dicom image folder');
set(handles.text2, 'String', dname);
files = dir(fullfile(dname, '*.dcm'));
dname = [dname '\'];
global indexSelected;
indexSelected = 1;
filePath = [dname files(1).name];
fileRead = dicomread(filePath);
imshow(fileRead, []);
end
function text2_Callback(hObject, eventdata, handles)
% hObject handle to text2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of text2 as text
% str2double(get(hObject,'String')) returns contents of text2 as a double
% --- Executes during object creation, after setting all properties.
end
function text2_CreateFcn(hObject, eventdata, handles)
% hObject handle to text2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
end
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global indexSelected;
global files;
global dname;
if(indexSelected == 1)
indexSelected = length(files);
filePath = [dname files(indexSelected).name];
fileRead = dicomread(filePath);
imshow(fileRead, []);
else
indexSelected = indexSelected - 1;
filePath = [dname files(indexSelected).name];
fileRead = dicomread(filePath);
imshow(fileRead, []);
end
end
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global indexSelected;
global files;
global dname;
if(indexSelected == length(files))
indexSelected = 1 ;
filePath = [dname files(1).name];
fileRead = dicomread(filePath);
imshow(fileRead,[]);
else
indexSelected = indexSelected + 1;
filePath = [dname files(indexSelected).name];
fileRead = dicomread(filePath);
imshow(fileRead,[]);
end
end
In short:
You can fix your problem by adding an axes in your GUI in the position (and with the size) in which you want to display the image
You should add in your callback some checks in order to catch the following situations:
the users selects Cancel when selecting the folder
the selected folder does not contains any .dcm files
pushbutton2 and pushbutton3 should be disabled when the GUI is opened and then enabled in the pushbutton1 callback if the users selected a right folder. You can disable them either using GUIDE or setting their enable property off in the GUI CreateFcn
Also you can avoid using global varaibles; you can actually store and share variables among the callback by using the guidata built-in function.
Adding the axes
After having added the axes (with, for example, tag axes1) you can set its appearence in the CreateFcn:
you can set a black background color using set function
you can remove X-axis and Y-axis ticks using set function as well
You can insert this callback in your code
% --- Executes during object creation, after setting all properties.
function axes1_CreateFcn(hObject, eventdata, handles)
% hObject handle to axes1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: place code in OpeningFcn to populate axes1
% Set axes background color to black
set(hObject,'color','k')
% Remove X-axis and Y-axis ticks
set(gca,'xtick',[],'ytick',[])
% Disable pushbutton2 and pusbutton 3 (they will be enabled in pushbutton1
% callback)
set(handles.pushbutton2,'enable','off')
set(handles.pushbutton3,'enable','off')
When the imshow function is called in your pushbutto callback the DICOM image will be displayed in the right position.
Adding checks on folder selection
You can simply test the value returned by uigetdir: it is set to 0 when the user pushes Cancel or
Using GUI data instead of flobal variables
You can store the varaibles you want to share among the callback adding them to the GUI data struct.
In the GUI OpeningFcn you can initialize (if needed) the varaibles you want to share by adding the following code:
% Add "indexSelected" to handles struct and initialize it
my_gui_data=guidata(gcf)
my_gui_data.indexSelected=0;
guidata(gcf,my_gui_data);
The function guidata is used at the beginning to get the GUI data from the GUI (at the beginning it only contains the GUI handles).
Then you can add the indexSelected to the GUI data struct and set the updated GUI data struct by calling again guidata.
When you need to retrieve and / or update the variables in the callback you just have to use the same approach. For example, to store the dname varaible in the pushbutton1_Callback
my_gui_data=guidata(gcf)
my_gui_data.dname=dname;
guidata(gcf,my_gui_data);
I've created a GUI (dcom_gui) to test the above suggestions:
function varargout = ui(varargin)
% UI MATLAB code for ui.fig
% UI, by itself, creates a new UI or raises the existing
% singleton*.
%
% H = UI returns the handle to a new UI or the handle to
% the existing singleton*.
%
% UI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in UI.M with the given input arguments.
%
% UI('Property','Value',...) creates a new UI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before ui_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to ui_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help ui
% Last Modified by GUIDE v2.5 21-Nov-2015 17:35:56
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #ui_OpeningFcn, ...
'gui_OutputFcn', #ui_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before ui is made visible.
function ui_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to ui (see VARARGIN)
% Choose default command line output for ui
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% Add "indexSelected" to handles struct and initialize it
my_gui_data=guidata(gcf)
my_gui_data.indexSelected=0;
guidata(gcf,my_gui_data);
% Disable pushbutton2 and pusbutton 3 (they will be enabled in pushbutton1
% callback)
set(handles.pushbutton2,'enable','off')
set(handles.pushbutton3,'enable','off')
% UIWAIT makes ui wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = ui_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get GUI data
my_gui_data=guidata(gcf)
% Use guidata data struct instead of "global" variables
% global files;
% global dname;
dname = uigetdir('Select the dicom image folder');
% Check for directory selection validity
if(dname == 0)
set(handles.text2,'string','Dir selection aborted')
else
set(handles.text2, 'String', dname);
files = dir(fullfile(dname, '*.dcm'));
% Check for files presence
if(length(files) == 0)
set(handles.text2,'string',['No ".dcm" file in ' dname ' folder'])
else
% Enable pushbutton2 and pusbutton 3
set(handles.pushbutton2,'enable','on')
set(handles.pushbutton3,'enable','on')
% Use guidata data struct instead of "global" variables
% global indexSelected;
% Not needed, use fullfile to build the full file name
% dname = [dname '\'];
indexSelected = 1;
% filePath = [dname files(1).name];
filePath = fullfile(dname,files(1).name);
fileRead = dicomread(filePath);
imshow(fileRead, []);
% Store GUI data
my_gui_data.dname=dname;
my_gui_data.files=files;
my_gui_data.indexSelected=indexSelected;
my_gui_data.filePath=filePath;
guidata(gcf,my_gui_data);
end
end
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Use guidata data struct instead of "global" variables
% global indexSelected;
%
% global files;
% global dname;
my_gui_data=guidata(gcf);
indexSelected=my_gui_data.indexSelected;
files=my_gui_data.files;
dname=my_gui_data.dname;
if(indexSelected == 1)
indexSelected = length(files);
% Use "fullfile" to build the file name
% filePath = [dname files(indexSelected).name];
filePath = fullfile(dname,files(indexSelected).name);
fileRead = dicomread(filePath);
imshow(fileRead, []);
else
indexSelected = indexSelected - 1;
% Use "fullfile" to build the file name
% filePath = [dname files(indexSelected).name];
filePath = fullfile(dname,files(indexSelected).name);
fileRead = dicomread(filePath);
imshow(fileRead, []);
end
% Store GUI data
my_gui_data.filePath=filePath;
my_gui_data.indexSelected=indexSelected;
guidata(gcf,my_gui_data);
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Use guidata data struct instead of "global" variables
% global indexSelected;
%
% global files;
% global dname;
my_gui_data=guidata(gcf);
indexSelected=my_gui_data.indexSelected;
files=my_gui_data.files;
dname=my_gui_data.dname;
if(indexSelected == length(files))
indexSelected = 1 ;
% Use "fullfile" to build the file name
% filePath = [dname files(1).name];
filePath = fullfile(dname,files(1).name);
fileRead = dicomread(filePath);
imshow(fileRead,[]);
else
indexSelected = indexSelected + 1;
% Use "fullfile" to build the file name
% filePath = [dname files(indexSelected).name];
filePath = fullfile(dname,files(indexSelected).name);
fileRead = dicomread(filePath);
imshow(fileRead,[]);
end
% Store GUI data
my_gui_data.filePath=filePath;
my_gui_data.indexSelected=indexSelected;
guidata(gcf,my_gui_data);
% --- Executes during object creation, after setting all properties.
function axes1_CreateFcn(hObject, eventdata, handles)
% hObject handle to axes1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: place code in OpeningFcn to populate axes1
% Set axes background color to black
set(hObject,'color','k')
% Remove X-axis and Y-axis ticks
set(gca,'xtick',[],'ytick',[])
The GUI just opened
The GUI with a DCOM image
Hope this helps.

Buttondownfcn gui axes

I have a Gui, that it has an axes...In the axes i can draw lines with plot.. but i want to select a line of the axes.. i have tried with buttondownfcn..but it doesn't work.. i have a button DELETE and its callback is:
hold all;
set(handles.axes6, 'HitTest', 'off');
set(handles.axes6,'ButtonDownFcn',('h = copyobj(gcbo,figure)'));
delete_object_axes = findobj(h, 'Type', 'line');
My code is:
% --- Executes on mouse press over axes background.
function axes6_ButtonDownFcn(~, ~, handles)
% hObject handle to axes6 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
hold all;
set(handles.axes6, 'HitTest', 'off');
set(handles.axes6, 'ButtonDownFcn', {#Delete_Callback, handles}');
% --- Executes on mouse press over figure background, over a disabled or
% --- inactive control, or over an axes background.
function figure1_WindowButtonDownFcn(~, ~, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
hold all;enter code here
global h;
set(handles.axes6, 'HitTest', 'off');
h = findobj(handles.axes6, 'Type', 'line');
set(h, 'ButtonDownFcn', {#Delete_Callback});
Help me..how can i select and delete a line??? or move??? there is some manner to do it????
It is very important for me..please!! help me!:)
Below is an example code that should help you achieve what you want, based on this discussion in MATLABcentral. What it does is checl if you clicked on a line, if you did, it modifies it, and if you click it again it is deleted. You can paste it in a file and run.
function init()
close all; clear variables; clc;
ax1=ezplot('sin(x)');
set(ax1,'ButtonDownFcn',#mouseClick);
end
function mouseClick(source, event)
if strcmp(get(source,'Type'),'line')
if get(source,'linewidth')==2
delete(source);
return
end
%// Store the handle "source" someplace
%// Demonstration modification:
set(source,'linewidth',2);
end
end
Edit: here's a bit more complicated example, showing how to save variables to the handles structure (a.k.a. guidata):
function init()
close all; clear variables; clc;
hLine(1) = ezplot('sin(x)'); hold all; hLine(2) = ezplot('cos(x)');
set(hLine(:),'ButtonDownFcn',#mouseClick);
grid on
end
function mouseClick(hObject, eventdata)
handles = guidata(gcf); %#1
try
if handles.delete_object_axes==hObject;
delete(hObject);
handles = rmfield(handles,'delete_object_axes');
else
set(handles.delete_object_axes,'linewidth',1);
set(hObject,'linewidth',2);
handles.delete_object_axes = hObject;
end
catch
if strcmp(get(hObject,'Type'),'line')
handles.delete_object_axes = hObject;
set(hObject,'linewidth',2);
guidata(gca, handles);
else
set(handles.delete_object_axes,'linewidth',1);
end
return;
end
guidata(gca, handles);
end

How to create a GUI to play, pause, fast forward and rewind video in MATLAB?

I am a newbie to MATLAB. I am trying to create a GUI to play, pause, fast forward and rewind an avi video frame by frame. At the moment I can play and pause the video, via a toggle button, but when I press play again the video plays from frame zero. I realise I need to store the frame number to be used the next time I press play but I don't know how to do this.
Any help would be much appreciated. I realise there is an implay option in MATLAB but I have some other code to implement which I have already got right and that is why I want to create my own GUI. Below is the code to pause/play the video.
My most recent code looks like,
function varargout = N_Play_Pause_2(varargin)
% N_PLAY_PAUSE_2 MATLAB code for N_Play_Pause_2.fig
% N_PLAY_PAUSE_2, by itself, creates a new N_PLAY_PAUSE_2 or raises the existing
% singleton*.
%
% H = N_PLAY_PAUSE_2 returns the handle to a new N_PLAY_PAUSE_2 or the handle to
% the existing singleton*.
%
% N_PLAY_PAUSE_2('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in N_PLAY_PAUSE_2.M with the given input arguments.
%
% N_PLAY_PAUSE_2('Property','Value',...) creates a new N_PLAY_PAUSE_2 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before N_Play_Pause_2_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to N_Play_Pause_2_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help N_Play_Pause_2
% Last Modified by GUIDE v2.5 29-Aug-2013 08:39:38
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #N_Play_Pause_2_OpeningFcn, ...
'gui_OutputFcn', #N_Play_Pause_2_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before N_Play_Pause_2 is made visible.
function N_Play_Pause_2_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to N_Play_Pause_2 (see VARARGIN)
% Choose default command line output for N_Play_Pause_2
handles.output = hObject;
handles.VidObj = VideoReader('x05.avi');
handles.nFrames = handles.VidObj.NumberOfFrames;
handles.videoPos = 1; %Current video position. Starts at 1.
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes N_Play_Pause_2 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = N_Play_Pause_2_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in togglebutton1.
function togglebutton1_Callback(hObject, eventdata, handles)
% hObject handle to togglebutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of togglebutton1
while get(hObject,'Value')
snapshot = read(handles.VidObj,handles.videoPos); % Here we use the stored video position
if handles.videoPos >= handles.nFrames % Protect your code not to go be number of frames available
break;
end
handles.videoPos=handles.videoPos+1; % Increment the stored position
imshow(snapshot),title(handles.videoPos);
end
guidata(hObject,handles) % Save the modifications done at the handles structure at the figure handle
set(hObject,'Value',false);
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if get(hObject,'Value')
snapshot = read(handles.VidObj,handles.videoPos); % Here we use the stored video position
handles.videoPos=handles.videoPos+1; % Increment the stored position
imshow(snapshot),title(handles.videoPos);
end
guidata(hObject,handles) % Save the modifications done at the handles structure at the figure handle
set(hObject,'Value',false);
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if get(hObject,'Value')
snapshot = read(handles.VidObj,handles.videoPos); % Here we use the stored video position
handles.videoPos=handles.videoPos-1; % Increment the stored position
imshow(snapshot),title(handles.videoPos);
end
guidata(hObject,handles) % Save the modifications done at the handles structure at the figure handle
set(hObject,'Value',false);
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if get(hObject,'Value')
snapshot = read(handles.VidObj,handles.videoPos); % Here we use the stored video position
handles.videoPos=handles.videoPos+10; % Increment the stored position
imshow(snapshot),title(handles.videoPos);
end
guidata(hObject,handles) % Save the modifications done at the handles structure at the figure handle
set(hObject,'Value',false);
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if get(hObject,'Value')
snapshot = read(handles.VidObj,handles.videoPos); % Here we use the stored video position
handles.videoPos=handles.videoPos-10; % Increment the stored position
imshow(snapshot),title(handles.videoPos);
end
guidata(hObject,handles) % Save the modifications done at the handles structure at the figure handle
set(hObject,'Value',false);
Final solution
So, I haven't seen that you updated your code. Seeing your code is quite easier, it seems that you are incrementing after you show your image, so if you push the play button it will show the image before
function varargout = N_Play_Pause2(varargin)
% N_PLAY_PAUSE2 MATLAB code for N_Play_Pause2.fig
% N_PLAY_PAUSE2, by itself, creates a new N_PLAY_PAUSE2 or raises the existing
% singleton*.
%
% H = N_PLAY_PAUSE2 returns the handle to a new N_PLAY_PAUSE2 or the handle to
% the existing singleton*.
%
% N_PLAY_PAUSE2('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in N_PLAY_PAUSE2.M with the given input arguments.
%
% N_PLAY_PAUSE2('Property','Value',...) creates a new N_PLAY_PAUSE2 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before N_Play_Pause2_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to N_Play_Pause2_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help N_Play_Pause2
% Last Modified by GUIDE v2.5 23-Aug-2013 13:50:30
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #N_Play_Pause2_OpeningFcn, ...
'gui_OutputFcn', #N_Play_Pause2_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before N_Play_Pause2 is made visible.
function N_Play_Pause2_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to N_Play_Pause2 (see VARARGIN)
handles.output = hObject;
handles.VidObj = VideoReader('x05.avi');
handles.nFrames = handles.VidObj.NumberOfFrames;
handles.videoPos = 1; % Current video position, starts at first frame.
snapshot = read(handles.VidObj,handles.videoPos); % Here we use the stored video position
imshow(snapshot),title(handles.videoPos);
% Choose default command line output for N_Play_Pause2
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes N_Play_Pause2 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = N_Play_Pause2_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in togglebutton1.
function togglebutton1_Callback(hObject, eventdata, handles)
% hObject handle to togglebutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of togglebutton1
while get(hObject,'Value') && handles.videoPos < handles.nFrames
handles.videoPos=handles.videoPos+1; % Increment the stored position
snapshot = read(handles.VidObj,handles.videoPos); % Here we use the stored video position
imshow(snapshot),title(handles.videoPos);
end
end
guidata(hObject,handles) % Save the modifications done at the handles structure at the figure handle
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if get(hObject,'Value')
handles.videoPos=handles.videoPos+1; % Increment the stored position
snapshot = read(handles.VidObj,handles.videoPos); % Here we use the stored video position
imshow(snapshot),title(handles.videoPos);
end
guidata(hObject,handles) % Save the modifications done at the handles structure at the figure handle
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if get(hObject,'Value') && handles.videoPos>1
handles.videoPos=handles.videoPos-1; % Increment the stored position
snapshot = read(handles.VidObj,handles.videoPos); % Here we use the stored video position
imshow(snapshot),title(handles.videoPos);
end
guidata(hObject,handles) % Save the modifications done at the handles structure at the figure handle
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if get(hObject,'Value') && handles.videoPos<handles.nFrames-9
handles.videoPos=handles.videoPos+10; % Increment the stored position
snapshot = read(handles.VidObj,handles.videoPos); % Here we use the stored video position
imshow(snapshot),title(handles.videoPos);
end
guidata(hObject,handles) % Save the modifications done at the handles structure at the figure handle
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if get(hObject,'Value') && handles.videoPos>10
handles.videoPos=handles.videoPos-10; % Increment the stored position
snapshot = read(handles.VidObj,handles.videoPos); % Here we use the stored video position
imshow(snapshot),title(handles.videoPos);
end
guidata(hObject,handles) % Save the modifications done at the handles structure at the figure handle
Seems you got confused a little bit with the coding, I've edited it and commented your errors.
They are preceded by % COMMENT:
function varargout = N_Play_Pause(varargin)
% N_PLAY_PAUSE MATLAB code for N_Play_Pause.fig
% N_PLAY_PAUSE, by itself, creates a new N_PLAY_PAUSE or raises the existing
% singleton*.
%
% H = N_PLAY_PAUSE returns the handle to a new N_PLAY_PAUSE or the handle to
% the existing singleton*.
%
% N_PLAY_PAUSE('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in N_PLAY_PAUSE.M with the given input arguments.
%
% N_PLAY_PAUSE('Property','Value',...) creates a new N_PLAY_PAUSE or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before N_Play_Pause_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to N_Play_Pause_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help N_Play_Pause
% Last Modified by GUIDE v2.5 13-Aug-2013 16:26:32
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #N_Play_Pause_OpeningFcn, ...
'gui_OutputFcn', #N_Play_Pause_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before N_Play_Pause is made visible.
function N_Play_Pause_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to N_Play_Pause (see VARARGIN)
% Choose default command line output for N_Play_Pause
handles.output = hObject;
handles.VidObj = VideoReader('x05.avi'); % COMMENT: PAY ATTENTION, 's' was missing!
handles.nFrames = handle.VidObj.NumberOfFrames;
handles.videoPos = 1; % Current video position, starts at first frame.
% Update handles structure
guidata(hObject, handles); % Here you are saving the handles method at the hObject, this is the handle from your GUI figure.
% COMMENT: In the original code here, you would save guidata twice, you didn't need that, just save guidata after you make ALL ALTERATIONS IN IT!
% --- Outputs from this function are returned to the command line.
function varargout = N_Play_Pause_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in togglebutton1.
function togglebutton1_Callback(hObject, eventdata, handles)
% hObject handle to togglebutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of togglebutton1
while get(hObject,'Value')
snapshot = read(handles.VidObj,handles.videoPos); % Here we use the stored video position
imshow(snapshot),title(double2str(handles.videoPos));
if handles.videoPos >= handles.nFrames % Protect your code not to go be number of frames available
break;
end
handles.videoPos=handles.videoPos+1; % Increment the stored position
end
guidata(hObject,handles) % Save the modifications done at the handles structure at the figure handle
Reading your question title I was like, what else you need? Do you also want a desert? But jokes a part, I was wrong, at least you have something working.
I am not an image specialist at matlab, but instead doing a = 0 in your callback function (and therefore reseting your video to start position), you will need to save your video position at your GUI. There are several ways of doing it, one way would be by using guidata, setappdata, or to pass it via arguments to your callbacks. You could call it a variable videoPos and add it to the handles struct that you store with guidata. Also save your nFrames variable in this struct.
The fast forward would be the same as you showed, but instead doing videoPos = videoPos + 1 you would do videoPos = videoPos + n, where n is the speed multiplier you want on the fast forward. To rewind, just decrement your videoPos, or reset it to 1, depending what you want.
Note: Don't forget to add checkers, you won't want your videoPos lesser than 0 or greater than nFrames.
On the function: N_Play_Pause_OpeningFcn Add the following data:
handle.VidObj = VideoReader('x05.avi');
handle.nFrames = VidObj.NumberOfFrames;
handle.videoPos = 1; % Current video position, starts at first frame.
% Update handles structure
guidata(hObject, handles); % Here you are saving the handles method at the hObject, this is the handle from your GUI figure.
Then, at your function togglebutton1_Callback do:
% --- Executes on button press in togglebutton1.
function togglebutton1_Callback(hObject, eventdata, handles)
% hObject handle to togglebutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of togglebutton1
% You won't need this line anymore a=0;
% If you would like to retrieve the stored guidata you would do it here, by doing **handles=guidata(hObject);** but this is not needed, the handles data stored at the figure handle is already given to you as the third argument internally by the matlab!
while get(hObject,'Value')
snapshot = read(VidObj,handles.videoPos); % Here we use the stored video position
imshow(snapshot),title(double2str(handles.videoPos));
if handles.videoPos >= handles.nFrames % Protect your code not to go be number of frames available
break;
end
handles.videoPos=handles.videoPos+1; % Increment the stored position
end
guidata(hObject,handles) % Save the modifications done at the handles structure at the figure handle
Note that you need to update the guidata everytime you quit your methods, so that you keep it updated and saved on the figure handle. One more detail is that the object you pass for the guidata don't need to be the figure handle, but any object holden by it, as the play button you have created. As in the guidata help:
GUIDATA(H, DATA) stores the specified data in the figure's
application data.
H is a handle that identifies the figure - it can be the figure
itself, or any object contained in the figure.
Now just add more buttons and work with the togglebutton1 method, the fastforward would be the same, but using instead of +1, +n.