Passing data from MATLAB GUI to another function - matlab

I have a matlab GUI (Compare2ImagesGUI) which gets called within another GUI (DistanceOrderGUI) and should return a variable based on some interaction with a user.
Here is a snippet of how the Compare2ImagesGUI gets called:
a=1;b=2;
handles.a = a;
handles.b = b;
result = Compare2ImagesGUI('DistanceOrderGUI', handles)
And here is what it does when it opens:
function Compare2ImagesGUI_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 Compare2ImagesGUI (see VARARGIN)
% Choose default command line output for Compare2ImagesGUI
%handles.output = hObject;
a = varargin{2}.a;
b = varargin{2}.b;
handles.a = a;
handles.b = b;
handles.ima = varargin{2}.ims{a};
handles.imb = varargin{2}.ims{b};
init(hObject,handles);
% UIWAIT makes Compare2ImagesGUI wait for user response (see UIRESUME)
uiwait(hObject);
this is the init function:
function init(hObject,handles)
imshow(handles.ima,'Parent',handles.axes1);
handles.current = handles.a;
% handles.ims=ims; handles.gt=gt;
% handles.folderN=folderN; handles.name=dirnames{folderN};
% Update handles structure
guidata(hObject, handles);
When the user finishes interacting he presses a button and the GUI should close and return the value to its calling function:
% --- 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)
figure1_CloseRequestFcn(hObject,handles);
% --- Outputs from this function are returned to the command line.
function varargout = Compare2ImagesGUI_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.current
delete(handles.Compare2ImagesGUI);
% --- Executes when user attempts to close figure1.
function figure1_CloseRequestFcn(hObject, 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)
if isequal(get(hObject,'waitstatus'),'waiting')
uiresume(hObject);
else
delete(hObject);
end
I followed instruction on how to structure this code found here and here, nonetheless I get this weird error and am really clueless on what to do about it:
Error using hg.uicontrol/get
The name 'waitstatus' is not an accessible property for an instance of
class 'uicontrol'.
Error in Compare2ImagesGUI>figure1_CloseRequestFcn (line 127)
if isequal(get(hObject,'waitstatus'),'waiting')
Error in Compare2ImagesGUI>pushbutton3_Callback (line 118)
figure1_CloseRequestFcn(hObject,handles);
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in Compare2ImagesGUI (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
#(hObject,eventdata)Compare2ImagesGUI('pushbutton3_Callback',hObject,eventdata,guidata(hObject))
Error using waitfor
Error while evaluating uicontrol Callback
Note that line 127 is in the function figure1_CloseRequestFcn(hObject, handles) function. Any suggestions?

Usually a CloseRequestFcn is not called via a uicontrol that you created, but rather when:
you close the figure using the built-in figure controls (e.g the "X" box at the top, or the figure menu)
close is called for your the figure
quit MATLAB
What happens is that pushbutton3_Callback passes its own handle rather the figure's handle in hObject to figure1_CloseRequestFcn. The problem is that the 'waitstatus' property only belongs to a figure.
The solution is to either modify pushbutton3_Callback to pass the figure handle, or modify pushbutton3_Callback to just use the figure handle. For example:
% --- Executes when user attempts to close figure1.
function figure1_CloseRequestFcn(hObject, eventdata, 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)
hFig = ancestor(hObject,'Figure');
if isequal(get(hFig,'waitstatus'),'waiting')
uiresume(hFig);
else
delete(hFig);
end
NOTE: I added and eventdata argument to figure1_CloseRequestFcn, which seemed to be missing from your code. (Normally it is defined as #(hObject,eventdata)guitest('figure1_CloseRequestFcn',hObject,eventdata,guidata(hObject))).

Related

Calling a function using a Matlab GUI pushbutton

I'm trying to call a function qrsdet(vecParam1,scaParam1,scaParam2) in GUIDE using a pushbutton startAnalysis. Here is the code:
GUI CODE:
% --- Executes just before GUIforUser is made visible.
function GUIforUser_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
-------
% remaining GUI code
-------
% pushbutton code to call function
function qrsdetfn_Callback(hObject, eventdata, handles)
hr = qrsdet(vecArg1,scaArg1,scaArg2);
textLabel = sprintf('%.2f', hr);
set(handles.heartratetext, 'String', hr);
guidata(hObject,handles)
I have defined a .m file called qrsdet.m, which resides in the same directory as my GUI. All three arguments are acquired from the user using the GUI. The issue is when I pass the arguments to my function I get the error:
Undefined function or variable 'vecArg1'.
I have stored vecArg1 in the handles structure in the matlab GUI. I've even tried using the following statement:
qrsdet(handles.vecArg1,scaArg1,scaArg2)
but this returns the error:
Reference to non-existent field 'vecArg1'
This is the pushbutton I'm using to load vecArg1
% --- Executes on button press
function pushbtnForvecArg1_Callback(hObject, eventdata, handles)
handles.fileloc = get(handles.filelocation,'String');
fileID = fopen(handles.fileloc);
handles.vecArg1 = fscanf(fileID,'%f',inf);
assignin('base','vecArg1',handles.vecArg1);
guidata(hObject,handles)
I'm pretty new to GUI design in Matlab, any pointers to what might be the issue?
I believe the problem is your input parameters.
When you start any function in MATLAB, your variables must be assigned a value. MATLAB GUIDE will not allow for variables to be used in the means that you have used vecArg1, vecArg2, and vecArg3. It essentially thinks that you have used a variable which does not exist.
I think the following code may work for you.
Set your variables using:
setappdata(hObject.Parent, 'vecArg1', desired_value_to_be_stored);
This will allow you to use the following code in a different section of the GUIDE file to retrieve this data:
data_to_be_used = getappdata(hObject.Parent, 'vecArg1');
It's a bit tedious but it should work.
~~~~~~~~~~~~~~~~~~~~~~~~
EDIT1: Demonstration of use of setappdata and getappdata
GUIDE m-file, the figurecontains:
pushbutton1 -> get the data & test
pushbutton2 -> set the data
function varargout = gui_example(varargin)
% GUI_EXAMPLE MATLAB code for gui_example.fig
% GUI_EXAMPLE, by itself, creates a new GUI_EXAMPLE or raises the existing
% singleton*.
%
% H = GUI_EXAMPLE returns the handle to a new GUI_EXAMPLE or the handle to
% the existing singleton*.
%
% GUI_EXAMPLE('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in GUI_EXAMPLE.M with the given input arguments.
%
% GUI_EXAMPLE('Property','Value',...) creates a new GUI_EXAMPLE or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before gui_example_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to gui_example_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_example
% Last Modified by GUIDE v2.5 10-Apr-2016 15:17:00
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #gui_example_OpeningFcn, ...
'gui_OutputFcn', #gui_example_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_example is made visible.
function gui_example_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_example (see VARARGIN)
% Choose default command line output for gui_example
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes gui_example wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = gui_example_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)
status = printvector(getappdata(hObject.Parent, 'vecArg1'));
disp(status);
% --- 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)
%Set vector argument
vectorArgument1 = [1.001; 1.002; 1.003; 1.004];
setappdata(hObject.Parent, 'vecArg1', vectorArgument1);
Function called on button press:
function [ status ] = printvector( vec1 )
disp('I am in the function')
for i = 1:length(vec1)
disp(vec1(i,1));
end
status = 'success';
end

matlab eventdata of uitable returned empty

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.

Matlab: set default values for GUI edit text and use them in push button callback

I'm trying to initialize a GUI (built with GUIDE) with default values and then, if the user does not change the defaults, use these values in a function triggered by a push button callback.
To do this, inside the _CreateFcn, I first store the default values within the handles, then set the default of the GUI with set(hObject, ...) and finally update the guidata with guidata(hObject, handles);
If the user changes the value, I store the updated value in the handles inside the _Callback function reading the value with get(hObject, ...) and updating the guidata with guidata(hObject, handles);
When the button is pushed, inside the button _Callback function, I extract the value from the handles.
What happen is the following:
if the user does not change the value on the GUI and simply pushes the button, what I read out from the variable stored in the handles is not the value of the variable, but the actual handle to the variable (for example: 27.0098876953125)
If, on the other hand, the user does change the value before pushing the button, then everything works fine and I get the actual variable value.
What am I missing?
Update
Following oro777 comment I've added the rest of the code for better analysis. I've also tried with a more recent (R2015b) version of MATLAB and the result is the same, with the difference that now the disp inside the button callback function shows the entire handle structure instead of just the id:
UIControl (ampmin) with properties:
Style: 'edit'
String: '1'
BackgroundColor: [1 1 1]
Callback: #(hObject,eventdata)GUI('ampmin_Callback',hObject,eventdata,guidata(hObject))
Value: 0
Position: [15.6000 14.6154 10.2000 1.6923]
Units: 'characters'
Use get to show all properties
I've also noticed the following:
- If I start the .fig file everything works fine
- If I push the run button on the .m file, the strange behavior described above occurs
Here is the code:
function varargout = GUI2(varargin)
% GUI2 MATLAB code for GUI2.fig
% GUI2, by itself, creates a new GUI2 or raises the existing
% singleton*.
%
% H = GUI2 returns the handle to a new GUI2 or the handle to
% the existing singleton*.
%
% GUI2('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in GUI2.M with the given input arguments.
%
% GUI2('Property','Value',...) creates a new GUI2 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI2 before GUI2_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to GUI2_OpeningFcn via varargin.
%
% *See GUI2 Options on GUIDE Tools menu. Choose "GUI2 allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help GUI2
% Last Modified by GUIDE v2.5 14-Aug-2015 10:39:46
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #GUI2_OpeningFcn, ...
'gui_OutputFcn', #GUI2_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 GUI2 is made visible.
function GUI2_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 GUI2 (see VARARGIN)
% Choose default command line output for GUI2
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes GUI2 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = GUI2_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 startAnalysis.
function startAnalysis_Callback(hObject, eventdata, handles)
% hObject handle to startAnalysis (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
disp(handles.ampmin)
function ampmin_Callback(hObject, eventdata, handles)
% hObject handle to ampmin (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 ampmin as text
% str2double(get(hObject,'String')) returns contents of ampmin as a double
handles.ampmin = str2double(get(hObject,'String'));
% Update handles structure
guidata(hObject, handles);
% --- Executes during object creation, after setting all properties.
function ampmin_CreateFcn(hObject, eventdata, handles)
% hObject handle to ampmin (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
handles.ampmin = 1.0;
disp(handles.ampmin)
set(hObject, 'String', num2str(handles.ampmin))
% Update handles structure
guidata(hObject, handles);
Thanks #Hoki, that was the problem. Since I didn't see any direct reference to handles.ampmin I didn't think it would be used to store the handle, but looking at the actual .fig exported code, it became apparent that indeed that's were the uicontrol handle is stored.

How to get varargout to output text from pushbuttons in Matlab GUI?

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

Error ploting bode , nyquist and nichols responses in gui matlab

I want to create a graphical interface with the GUIDE Matlab that displays diffrent responses of a transfer function : step , impulse , bode , nyquist and nichols responses.
it works fine for the step and impulse responses but with nyquist , bode and nichols it dosen't work , i should add 'squeeze' to the 'plot' function but it's not exactly the correct response !
this is the error when i try with plot only :
??? Error using ==> plot Data may not have more than 2 dimensions
This is th LTI.fig file
The following code is the content of the .m file
function varargout = LTI(varargin)
% LTI M-file for LTI.fig
% LTI, by itself, creates a new LTI or raises the existing
% singleton*.
%
% H = LTI returns the handle to a new LTI or the handle to
% the existing singleton*.
%
% LTI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in LTI.M with the given input arguments.
%
% LTI('Property','Value',...) creates a new LTI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before LTI_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to LTI_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 LTI
% Last Modified by GUIDE v2.5 24-Nov-2014 10:41:38
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #LTI_OpeningFcn, ...
'gui_OutputFcn', #LTI_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 LTI is made visible.
function LTI_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 LTI (see VARARGIN)
% Choose default command line output for LTI
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes LTI wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = LTI_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;
function numerateur_Callback(hObject, eventdata, handles)
% hObject handle to numerateur (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 numerateur as text
% str2double(get(hObject,'String')) returns contents of numerateur as a double
% --- Executes during object creation, after setting all properties.
function numerateur_CreateFcn(hObject, eventdata, handles)
% hObject handle to numerateur (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
function denumerateur_Callback(hObject, eventdata, handles)
% hObject handle to denumerateur (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 denumerateur as text
% str2double(get(hObject,'String')) returns contents of denumerateur as a double
% --- Executes during object creation, after setting all properties.
function denumerateur_CreateFcn(hObject, eventdata, handles)
% hObject handle to denumerateur (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 rep_indic.
function rep_indic_Callback(hObject, eventdata, handles)
% hObject handle to rep_indic (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
num= str2num(get(handles.numerateur,'String'));
denum = str2num(get(handles.denumerateur,'String'));
G=tf([num],[denum]);
axes(handles.figure)
plot(step(G))
grid on
% --- Executes on button press in lieu_bode.
function lieu_bode_Callback(hObject, eventdata, handles)
% hObject handle to lieu_bode (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
num= str2num(get(handles.numerateur,'String'));
denum = str2num(get(handles.denumerateur,'String'));
G=tf([num],[denum]);
axes(handles.figure)
plot(squeeze(bode(G)))
grid on
% --- Executes on button press in rep_impuls.
function rep_impuls_Callback(hObject, eventdata, handles)
% hObject handle to rep_impuls (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
num= str2num(get(handles.numerateur,'String'));
denum = str2num(get(handles.denumerateur,'String'));
G=tf([num],[denum]);
axes(handles.figure)
plot(impulse(G))
grid on
% --- Executes on button press in lieu_nyquist.
function lieu_nyquist_Callback(hObject, eventdata, handles)
% hObject handle to lieu_nyquist (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
num= str2num(get(handles.numerateur,'String'));
denum = str2num(get(handles.denumerateur,'String'));
G=tf([num],[denum]);
axes(handles.figure)
plot(s(nyquist(G)))
grid on
% --- Executes on button press in lieu_nichols.
function lieu_nichols_Callback(hObject, eventdata, handles)
% hObject handle to lieu_nichols (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
num= str2num(get(handles.numerateur,'String'));
denum = str2num(get(handles.denumerateur,'String'));
G=tf([num],[denum]);
axes(handles.figure)
plot(squeeze(nichols(G)))
grid on
You don't provide the *.fig file to go with your code, but I suspect I know what the problem is: bode, nyquist and nichols produce the plot automatically, you don't need to call the plot function. Check the documentation for the correct way to call these functions. In your GUI, replace:
plot(squeeze(bode(G))) by bode(G)
plot(s(nyquist(G))) [sic] by nyquist(G)
plot(squeeze(nichols(G))) by nichols(G)
EDIT based on comments
I think all you need to do is
num= str2num(get(handles.numerateur,'String'));
denum = str2num(get(handles.denumerateur,'String'));
G=tf([num],[denum]);
axes(handles.figure)
step(G)
grid on
and the same applies for bode, nyquist, nichols and impulse, i.e. just create a set of axes in the figure and the plot will be displayed in those axes by default.