Related
So i created GUI in guide that looks like this:
GUI
I want to access data from radio button and then change the variables in my simulation (Bitrate and Modulation are the button groups, Improvement is a single radio button). For example - in the simulation I have a variable Rs=1e9, so when 1Gbps button is selected I want it to remain 1e9, but if 10Gbps button is selected I want it to change its value to 10e9.
Then after hitting Start button I want to start my simulation (which is in different .m file) with given parameters. How can I do it ? (I know about handles idea in matlab, but I don't know how to pass value to the simulation)
That's the code that controls gui - generated by guide. I added some code that starts simulation and close gui window.
function varargout = gui_final(varargin)
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #gui_final_OpeningFcn, ...
'gui_OutputFcn', #gui_final_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_final is made visible.
function gui_final_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 unrecognized PropertyName/PropertyValue pairs from the
% command line (see VARARGIN)
% Choose default command line output for gui_final
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes gui_final wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = gui_final_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 start.
function start_Callback(hObject, eventdata, handles)
% hObject handle to start (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
clc
close all
message = sprintf('Wait - this is a very long simulation!\nClick the OK button and wait');
uiwait(msgbox(message));
evalin('base', 'simulation');
% --- Executes on button press in dfe.
function dfe_Callback(hObject, eventdata, handles)
% hObject handle to dfe (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 dfe
% --- Executes on button press in ook.
function ook_Callback(hObject, eventdata, handles)
% hObject handle to ook (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 ook
You can do it using the object "hObject"
Like this: Suppose that you have an slider, everytime the slider is moved the callback is called. You can use it to store a variable. Then, when you press a button you want to use that data for whatever. The code is:
function slider_callback(hObject,eventdata)
sval = hObject.Value;
diffMax = hObject.Max - sval;
data = struct('val',sval,'diffMax',diffMax);
hObject.UserData = data;
% For R2014a and earlier:
% sval = get(hObject,'Value');
% maxval = get(hObject,'Max');
% diffMax = maxval - sval;
% data = struct('val',sval,'diffMax',diffMax);
% set(hObject,'UserData',data);
end
function button_callback(hObject,eventdata)
h = findobj('Tag','slider1');
data = h.UserData;
% For R2014a and earlier:
% data = get(h,'UserData');
display([data.val data.diffMax]);
end
REFERENCE: Matlab Docs
UPDATE
In your case you can do it with another approach if you want. When you press the button START you read the state of your radio buttons, and pass the appropiate value to your simulation_function that I suppose it is called "simulation". In the following example I suppose that you have a button-group called (tag): uibuttongroup1, and inside you have two buttons: radiobutton1 and radiobutton2
% --- Executes on button press in start.
function start_Callback(hObject, eventdata, handles)
% hObject handle to start (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%Check which radio button is pressed
switch get(get(handles.uibuttongroup1,'SelectedObject'),'Tag')
case 'radiobutton1', myParameter = 1e9;
case 'radiobutton2', myParameter = 10e9;
end
%Execute simulation
clc
close all
message = sprintf('Wait - this is a very long simulation!\nClick the OK button and wait');
uiwait(msgbox(message));
evalin('base', 'simulation(myParameter)');
I'm trying to run a GUIDE app in Matlab, but I encounter a problem: when I try to access the location of the selected cell in a ui table, the variable holding it (eventdata.Indices) quickly changes back to an empty vector.
Here's my CellSelectionCallback function:
function varargout = ChannelMatrix(varargin)
% CHANNELMATRIX MATLAB code for ChannelMatrix.fig
% CHANNELMATRIX, by itself, creates a new CHANNELMATRIX or raises the existing
% singleton*.
%
% H = CHANNELMATRIX returns the handle to a new CHANNELMATRIX or the handle to
% the existing singleton*.
%
% CHANNELMATRIX('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in CHANNELMATRIX.M with the given input arguments.
%
% CHANNELMATRIX('Property','Value',...) creates a new CHANNELMATRIX or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before ChannelMatrix_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to ChannelMatrix_OpeningFcn via varargin.
%
% *See GUI Options on GUIDEs 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 ChannelMatrix
% Last Modified by GUIDE v2.5 14-Oct-2015 17:06:14
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #ChannelMatrix_OpeningFcn, ...
'gui_OutputFcn', #ChannelMatrix_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 ChannelMatrix is made visible.
function ChannelMatrix_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 ChannelMatrix (see VARARGIN)
clc
% Choose default command line output for ChannelMatrix
handles.output = hObject;
%CONNECT TO SWITCHER
matrixes=cell(12,6,26);
setappdata(0,'matrixes',matrixes);
set(handles.listbox1,'Value',1);
setappdata(0,'index_selected',1); %force Alpha for deafult selection
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes ChannelMatrix wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = ChannelMatrix_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 when selected cell(s) is changed in uitable1.
function uitable1_CellSelectionCallback(hObject, eventdata, handles)
% hObject handle to uitable1 (see GCBO)
% eventdata structure with the following fields (see UITABLE)
% Indices: row and column indices of the cell(s) currently selecteds
% handles structure with handles and user data (see GUIDATA)
% --- Executes when selected cell(s) is changed in uitable1.
try
currow = eventdata.Indices(1);
curcol = eventdata.Indices(2);
adata=get(handles.uitable1,'Data');
if adata{currow,curcol} == 'V'
adata{currow,curcol} = '';
else
adata{currow,curcol} = 'V';
end
set(hObject,'Data',adata);
end
% --- Executes on button press in cls_ch.
function cls_ch_Callback(hObject, eventdata, handles)
% hObject handle to cls_ch (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
index_selected = getappdata(0,'index_selected'); %get the number of selected item in listbox
matrixes = getappdata(0,'matrixes'); %get current selection of measurments
table = get(handles.uitable1,'data'); %get the selected channels for this measure
for i=3:14
for j=1:6
if table{i-2,j}=='V'
matrixes{i-2,j,index_selected}='V'; %preform scan and check - add to matrixes selcted values
end
end
end
%save changes
set(handles.uitable3,'Data',matrixes(:,:,index_selected));
setappdata(0,'matrixes',matrixes);
guidata(hObject, handles);
% --- Executes on button press in opn_ch.
function opn_ch_Callback(hObject, eventdata, handles)
% hObject handle to opn_ch (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
index_selected = getappdata(0,'index_selected'); %get the number of selected item in listbox
matrixes = getappdata(0,'matrixes'); %get current selection of measurments
table = get(handles.uitable1,'data'); %get the selected channels for this measure
for i=3:14
for j=1:6
if table{i-2,j}=='V'
matrixes{i-2,j,index_selected}=''; %preform scan and check - add to matrixes selcted values
end
end
end
%save changes
set(handles.uitable3,'Data',matrixes(:,:,index_selected));
setappdata(0,'matrixes',matrixes);
guidata(hObject, handles);
% --- Executes on button press in opn_all.
function opn_all_Callback(hObject, eventdata, handles)
% hObject handle to opn_all (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
matrixes=cell(12,6,26);
setappdata(0,'matrixes',matrixes); %sets up a new matrix
set(handles.uitable1,'Data',cell(12,6));
set(handles.uitable3,'Data',cell(12,6));
set(handles.listbox1,'Value',1);
guidata(hObject, handles);
% --- Executes on button press in clr_selection_btn.
function clr_selection_btn_Callback(hObject, eventdata, handles)
% hObject handle to clr_selection_btn (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.uitable1,'Data',cell(12,6));
% --- Executes on selection change in listbox1.
function listbox1_Callback(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns listbox1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from listbox1
matrixes=getappdata(0,'matrixes');
index_selected=get(hObject,'Value');
setappdata(0,'index_selected',index_selected);
set(handles.uitable3,'Data',matrixes(:,:,index_selected));
set(handles.uitable1,'Data',cell(12,6));
guidata(hObject, handles);
% --- Executes during object creation, after setting all properties.
function listbox1_CreateFcn(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: listbox 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
What's wrong here?
Thanks!
I think there is nothing anything wrong in the definition of your uitable1_CellSelectionCallback function.
I've built a simple GUI consisting of just a uitable and executing step by step the callback in debug mode this is what actually happens:
when you left-click on a cell's table, the uitable1_CellSelectionCallback is correctly called
everything goes right up to the set(hObject,'Data',adata); instruction and eventdata.Indices array contains the row and column index of the selected cell (ref. to the Function Call Stack in the following picture)
as the set(hObject,'Data',adata); function is executed, automatically a new call to uitable1_CellSelectionCallback is made by MatLab (probably somehow triggered by set. In the next picture, you can see the second (recursive) call to uitable1_CellSelectionCallback in the Function Call Stack and the white and green pair of arrows in the edit window the debug mode
since the second call is not generated by an actual user selection of a cell, the eventdata.Indices is empty therefore an error is generated when the currow = eventdata.Indices(1); is executed
So, this is why when you click on a cell, its content is correctly set to "V" or to an empty string as you expeced but, at the same time (actually, after the time MatLab needs to make second call to uitable1_CellSelectionCallback) the GUI crashes.
I do not know if this might be considered a MatLab's bug, nevertheless I did not find any way to prevent the second call to uitable1_CellSelectionCallback.
A possible solution, not so far from yours (using try-catch) could be insert the present callback code in a if block checking if isempty(eventdata.Indices) (that is to replace try with if).
This will prevent the second call be "effective" and the generation of the error, which is not avoided by using try-catch (even if does not prevent the second call to happen).
Hope this helps.
I have two GUI's with the figure-tags mainWindow and annotatorWindow. I want to pass data between the two windows. When I copy the data from mainWindow to annotatorWindow (see copyData_Callback), it works perfectly. But when I want to write the data back to mainWindow (see saveData_Callback), I get the error "Matrix indices must be full double". I'm not entirely sure what this even means, any help is appreciated. The code of interest is below.
CALLBACKS UNDER annotatorWindow
% --- Executes on button press in copyData.
function copyData_Callback(hObject, eventdata, handles)
% hObject handle to copyData (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
h = findobj('Tag', 'mainWindow');
if ~isempty(h)
pData = guidata(h)
handles.UserData = pData.UserData
end
guidata(hObject, handles);
return
% --- Executes on button press in saveData.
function saveData_Callback(hObject, eventdata, handles)
% hObject handle to saveData (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
h = findobj('Tag', 'mainWindow');
if ~isempty(h)
guidata(h).UserData = handles.UserData
end
guidata(hObject, handles);
return
Figured it out! To pass data between two GUI's, you can only ever pull data from a GUI. You can never write data to a GUI from another GUI. I created a function in mainWindow called saveData (see below). I then called the saveData function from annotatorWindow allowing me to pass data back and forth.
mainWindow
function saveData(hObject, handles)
h = findobj('Tag', 'annotatorWindow');
if ~isempty(h)
aData = guidata(h)
handles.UserData = aData.UserData
end
guidata(hObject, handles);
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
annotatorWindow
% --- Executes on button press in saveData.
function saveData_Callback(hObject, eventdata, handles)
% hObject handle to saveData (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
h = findobj('Tag', 'mainWindow');
annotatorGUI('saveData', h, guidata(h));
return
I am trying to create a GUI using GUIDE where it allows the user to pick one of two pushbuttons. The strings in the pushbuttons will vary each time (I haven't automated this yet), but when any of the pushbuttons is pushed, I'd like the GUI to put out the string as an output variable.
I have the code to where it shows the output in the workspace, but the handles are deleted before the OutputFn is called. Any suggestions on how to fix this?
Also, I'd like to use a 'Next' button. Ideally this should pull up the next two texts to be displayed on the pushbuttons, in an iterative manner, but I'd be happy to move past the hurdle of getting the output for now.Here is what I have so far:
function varargout = Select_A_B(varargin)
% SELECT_A_B MATLAB code for Select_A_B.fig
% SELECT_A_B, by itself, creates a new SELECT_A_B or raises the existing
% singleton*.
%
% H = SELECT_A_B returns the handle to a new SELECT_A_B or the handle to
% the existing singleton*.
%
% SELECT_A_B('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in SELECT_A_B.M with the given input arguments.
%
% SELECT_A_B('Property','Value',...) creates a new SELECT_A_B or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Select_A_B_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Select_A_B_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 Select_A_B
% Last Modified by GUIDE v2.5 18-Jun-2015 15:12:42
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #Select_A_B_OpeningFcn, ...
'gui_OutputFcn', #Select_A_B_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 Select_A_B is made visible.
function Select_A_B_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 Select_A_B (see VARARGIN)
% Choose default command line output for Select_A_B
handles.output = hObject;
handles.string = '';
if isempty(varargin)
varargin{1} = 1;
varargin{2} = 1;
end
text1 = varargin{1};
text2 = varargin{2};
A = {'Apple';'Orange'};
B = {'Football';'Basketball'};
set(handles.pushbutton1,'string',A(text1)); % wait until we're ready
set(handles.pushbutton2,'string',B(text2)); % wait until we're ready
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Select_A_B wait for user response (see UIRESUME)
uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = Select_A_B_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} = hObject;
varargout{2} = handles.string;
% --- 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)
selectedButton = get(hObject,'String')
set(handles.string,'String',selectedButton);
guidata(hObject, handles);
close(gcf);
% --- 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)
selectedButton = get(hObject,'String')
set(handles.string,'String',selectedButton);
guidata(hObject, handles);
close(gcf);
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (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 edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (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
% --- 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)
% --- Executes during object creation, after setting all properties.
function pushbutton1_CreateFcn(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
I have the answer to my question. A summary of changes:
Added uiresume (after adding uiwait in the Opening function) after each Callback
Deleted close (gcf) after each Callback; if the figure closes, then hObject and handles in guidata are no longer accessible to the Output function
Created a handle structure to contain the text of interest and saved that in
guidata after each Callback function
Saved the output from guidata to a .mat file in the Output function
% --- 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)
selectedButton = get(hObject,'String')
handles.string = selectedButton;
guidata(hObject, handles);
uiresume(handles.figure1);
% close(gcf);
function varargout = Select_A_B_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} = hObject;
varargout{2} = handles.string;
save 'guioutput'
delete(hObject)
Learnings from the post experience:
I should’ve been more specific regarding my question. Hopefully I’ll
get better with more experience and learning the correct ‘lingo’ to
use.
I should’ve pointed out in my first post that I did spend
considerable time looking at multiple blogs and sites on Matlab GUI,
guide, and guidata. Frankly, none of the sites that I found addressed
my specific question.
This post is the one that finally helped me figure out my mistake:
http://www.mathworks.com/matlabcentral/answers/141809-issue-with-gui-editbox-callback
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
this is what i have so far
as you can see if i use save as i am using right now then i am saving the original image how would i save a modified image after applying one of the filters like black and white.
function varargout = testme(varargin)
% TESTME MATLAB code for testme.fig
% TESTME, by itself, creates a new TESTME or raises the existing
% singleton*.
%
% H = TESTME returns the handle to a new TESTME or the handle to
% the existing singleton*.
%
% TESTME('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in TESTME.M with the given input arguments.
%
% TESTME('Property','Value',...) creates a new TESTME or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before testme_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to testme_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 testme
% Last Modified by GUIDE v2.5 13-Oct-2014 13:17:45
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #testme_OpeningFcn, ...
'gui_OutputFcn', #testme_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 testme is made visible.
function testme_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 testme (see VARARGIN)
% Choose default command line output for testme
handles.fileLoaded = 0;
handles.fileLoaded2 = 0;
%-----------------------------------------------------------------------
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes testme wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = testme_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 loadbutton.
function loadbutton_Callback(hObject, eventdata, handles)
% hObject handle to loadbutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global im im2
[path,user_cance]=imgetfile();
if user_cance
msgbox(sprintf('Error'),'Error','Error');
return
end
im=imread(path);
im=im2double(im); %converts to double
im2=im; %for backup process :)
axes(handles.axes1);
imshow(im);
axes(handles.axes2);
hist(im);
%-----------------------------------------------------------------------
% --- Executes on button press in resetbutton.
function resetbutton_Callback(hObject, eventdata, handles)
% hObject handle to resetbutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global im2
axes(handles.axes1);
imshow(im2);
axes(handles.axes2);
hist(im2);
% --- Executes on button press in negativebutton.
function negativebutton_Callback(hObject, eventdata, handles)
% hObject handle to negativebutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global im
imblack=im;
imblack=1-im;
axes(handles.axes1);
imshow(imblack);
axes(handles.axes2);
hist(imblack);
% --- Executes on button press in greybutton.
function greybutton_Callback(hObject, eventdata, handles)
% hObject handle to greybutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global im
imgray=(im(:,:,1)+im(:,:,2)+im(:,:,2))/3;
axes(handles.axes1);
imshow(imgray);
axes(handles.axes2);
hist(imgray);
% --- 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
global im2
val=0.5*get(hObject,'Value')-0.5;
imbright=im2+val;
axes(handles.axes1);
imshow(imbright);
axes(handles.axes2);
hist(imbright);
% --- 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 savebutton.
function savebutton_Callback(hObject, eventdata, handles)
% hObject handle to savebutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[file,path]=uiputfile({'*.bmp','BMP'},'Save Image As');
f=getframe(handles.axes1);
[x,map]=frame2im(f);
imwrite(x,fullfile(path, file),'bmp');
% --- Executes on button press in black.
function black_Callback(hObject, eventdata, handles)
% hObject handle to black (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
axes(handles.axes1);
global im
xb=im;
b=im2bw(xb);
imshow(b);
There are several ways to do this, but maybe a good way is to store the image (after applying a filter) in the figure's application data. You can do this with guidata.
After each filter you store the image;
% --- Executes on button press in black.
function black_Callback(hObject, eventdata, handles)
% hObject handle to black (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
axes(handles.axes1);
global im
xb=im;
b=im2bw(xb);
imshow(b);
% Store the image
handles.image = b;
guidata(hObject, handles);
Then in your savebutton_Callback, simply refer to handles.image;