Storing data in nested functions in GUIDE, Matlab - matlab

I'm trying to create a simple gui in matlab and have some problems. The main idea of this simplified program is that I have a slider, it determines the frequency of a sine wave. Two plots show the resulting signal. A high res in 44100 Hz, and a low res in 100 Hz. Another slider determines the length of the signal being plotted (also this measured in Hz). The original program has quite a few more sliders to add more frequencies, but since I didn't think it was important I tried to scale it back for this question. The resulting code is still quite large, sorry for that.
My problem is it doesn't always update, and I get error messages. I try to store everything in handles, since I think this is how you're supposed to do it. handles.lowresx contains the low res time scale, handles.highresx contains the high res time scale. A temporary lowresy and highresy is then created in the function calcandplot(handles). Every time the time interval slider is moved, the function recalcx(hObject, newhz, handles) is called from the slider movement callback and a new lowresx and lowresy is calculated. It is then stored in guidata(hObject,handles) (I would hope) and these are used to calculate new lowresy and highresy for plotting. It doesn't seem to be stored though.
I'm not sure how to save the data now when it is a nested function inside the callback function. Am I supposed to call guidata(hObject, handles) all the way up in the call stack, meaning I have to pass down hObject as an argument to every function? Or only in the most inner function? I've tried both and none really work. And is it enough to just calculate lowresy and highresy and plot them, if I don't need them later, or should I save them in handles too, to make everything work right?
Do I have to call guidata(handles) after calling set(handles.intervaltext, 'String', num2str(val)) or does it update itself?
And I have a question about handles. The way I understand it a copy is created and passed down every time a function is called. Does this pose some kind of restrictions on how big data structures you can save there for it to be efficient? If a copy is created for every gui component when some kind of event is called on it (mouseover, key pressed etc) I can certainly see how things can get sluggish. Any tips for how to handle this?
Error message:
Reference to non-existent field 'lowresx'.
Error in gui>calcandplot (line 85)
lowresy = wave(handles.lowresx, handles.freq1);
Error in gui>intervalslider_Callback (line 106)
calcandplot(handles);
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 12-Oct-2016 14:18:38
% 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)
handles.freq1 = 0;
handles.lowsr = 1000;
handles.sr = 44100;
handles.lenhz = 220;
recalcx(hObject, handles.lenhz, handles);
'recalculated'
% 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{1} = handles.output;
% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
val = get(hObject,'Value');
handles.freq1 = val;
guidata(hObject, handles);
set(handles.text1, 'String', num2str(val));
calcandplot(handles);
% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
function calcandplot(handles)
lowresy = wave(handles.lowresx, handles.freq1);
highresy = wave(handles.highresx, handles.freq1);
axes(handles.axes1);
plot(handles.lowresx,lowresy, 'o');
axes(handles.axes2);
plot(handles.highresx,highresy, 'o');
function y = wave(x, freq1)
% x in sec
y = sin(x*freq1);
% --- Executes on slider movement.
function intervalslider_Callback(hObject, eventdata, handles)
val = get(hObject,'Value');
recalcx(hObject, val, handles);
strcat('val is now ', num2str(val))
strcat('handles.lenhz is now', num2str(handles.lenhz))
guidata(hObject, handles);
set(handles.intervaltext, 'String', num2str(val));
handles
calcandplot(handles);
% --- Executes during object creation, after setting all properties.
function intervalslider_CreateFcn(hObject, eventdata, handles)
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
function recalcx(hObject, newhz, handles)
handles.lenhz = newhz;
handles.lowresx = (0:1/handles.lowsr:(2*pi)/handles.lenhz)';
handles.highresx = (0:1/handles.sr:(2*pi)/handles.lenhz)';
strcat('inside handles is', num2str(handles.lenhz))
guidata(hObject, handles);
strcat('inside handles again is', num2str(handles.lenhz))

The issue is that within recalcx, you are modifying handles by adding the field lowresx. You (correctly) save this to the guidata using guidata(hObject, handles).
The issue is that in the calling function (gui_OpeningFcn), you then save a different handles struct that doesn't have those fields to the guidata since the modifications to the handles structure within recalcx aren't propagated back to the calling function.
It is this handles struct without those fields that is then passed around your GUI and causes the error you're seeing.
One option for how to fix this would be to have recalcx return the modified handles struct
function handles = recalcx(hObject, newhz, handles)
handles.lenhz = newhz;
handles.lowresx = (0:1/handles.lowsr:(2*pi)/handles.lenhz)';
handles.highresx = (0:1/handles.sr:(2*pi)/handles.lenhz)';
strcat('inside handles is', num2str(handles.lenhz))
guidata(hObject, handles);
strcat('inside handles again is', num2str(handles.lenhz))
end
And then the calling function can have an updated version
function gui_OpeningFcn(hObject, eventdata, handles, varargin)
handles.freq1 = 0;
handles.lowsr = 1000;
handles.sr = 44100;
handles.lenhz = 220;
handles = recalcx(hObject, handles.lenhz, handles);
% Choose default command line output for gui
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);

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.

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: 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.

Passing outside variables to MATLAB GUIDE

My program loads data from a file and produces a graph, the user clicks on an area of interest and then analysis is done and a new graph is produced. The program continues asking the user to click on the image until the user presses e to exit the program.
I want the graph that is produced to be a GUI that takes data from my program but I seem to have trouble transferring that data into the GUI function. Here is a quick example of what my program looks like:
load(data)
plot(x,y)
while 1%so that it continues asking for user interaction
figure(1)
'click on the point you want or press e to exit'
[x1,y1,key]=ginput(1)
f=score(x1,y1)
%the above is a different function that gives us the data that I want to graph,
%that are called xf,yf
%GUI plot
figure(1)
test_gui(xf,yf)
if (key == 'e')
display('End')
break;
else
display('next point')
end
end
My test_gui.m looks like this:
function varargout = test_gui(varargin)
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #test_gui_OpeningFcn, ...
'gui_OutputFcn', #test_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 fft_guide is made visible.
function test_gui_OpeningFcn(hObject, eventdata, handles, varargin)
% Choose default command line output for test_gui
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes test_gui wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = test_gui_OutputFcn(hObject, eventdata, handles)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
plot (xf,yf)
The problem is that when I click on the "Push" button, it does not graph anything so there must be something wrong in the way I pass the xf, yf variables. I was wondering if anyone has any ideas about what I am doing wrong, I have not used GUIDE before and it seems I'm lost.
From the looks of your code, xf and yf are never defined, only f (the result of score). So that could be why you can't see any plots.
Supposing that score dumps xf and yf into the workspace, you must first define them from varargin and then pass them to the callback function using handles, as Werner commented.
% --- Executes just before fft_guide is made visible.
function test_gui_OpeningFcn(hObject, eventdata, handles, varargin)
xf = varargin{0}; yf = varargin{1}; % Get xf and yf from input
handles.xf = xf; handles.yf = yf; % Put the values in handles
guidata(hObject,handles); % Save handles so you can use it anywhere in the GUI
And in the callback:
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
plot (handles.xf,handles.yf)
I believe this should work, supposing xf and yf are being correctly define before being passed to the GUI function.

GUI help MATLAB

So after completing some basic image processing programs, including inversion/mirroring/filtering etc, I want to create a GUI that includes buttons that could carry out each of these programs on an image that would be opened. After fiddling around with 'guide' I have gotten a bit lost.
All of these callbacks/createfcn/buttondownfcn have me a little bit confused, and I cant seem to get my syntax correct.
Ive tried various methods to make a button correspond to the push of a button including
function X %just adding the name of a function after the green comment lines describing the three variables
But that does not yield any results. My question is, how would I program a button to correspond with a function that I have previously created. Am I simply missing a certain function or am I not understanding some broader concept?
Sorry if the question is a bit vague, my knowledge on GUI's is rather small.
When you create a uiobject in guide, a callback function will be automatically generated in the corresponding m-file. Place your function call within that callback, and it will be executed when the button is pressed.
If you have other inputs, such as a text box or a select box, you can access them from the callback to read in the variable input.
For example, if you've got a textbox with the tag textbox1, and a function that you want to use the texbox content for, myfun, place the following under your button's callback function:
str = get(handles.textbox1,'String');
myfun(str);
OK, I generally don't care for GUIDE GUI's, but here's a really simple example:
function varargout = untitled(varargin)
% UNTITLED M-file for untitled.fig
% UNTITLED, by itself, creates a new UNTITLED or raises the existing
% singleton*.
%
% H = UNTITLED returns the handle to a new UNTITLED or the handle to
% the existing singleton*.
%
% UNTITLED('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in UNTITLED.M with the given input arguments.
%
% UNTITLED('Property','Value',...) creates a new UNTITLED or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before untitled_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to untitled_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 untitled
% Last Modified by GUIDE v2.5 18-Jun-2013 08:52:25
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #untitled_OpeningFcn, ...
'gui_OutputFcn', #untitled_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 untitled is made visible.
function untitled_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 untitled (see VARARGIN)
% Choose default command line output for untitled
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes untitled wait for user response (see UIRESUME)
% uiwait(handles.figure1);
end
% --- Outputs from this function are returned to the command line.
function varargout = untitled_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)
% get a handle to the text field
textDisp = get(handles.text1,'String');
if( strcmp(textDisp{1},'Static Text' ) )
set(handles.text1,'String',{'Checkout my new text'});
elseif( strcmp(textDisp{1},'Checkout my new text') )
set(handles.text1,'String',{'Clicked it 1 time'})
else
dd = sscanf(textDisp{1},'Clicked it %d time');
dd = dd + 1;
set(handles.text1,'String',{['Clicked it ' num2str(dd) ' time']})
end
end
It's going to run off a really simple figure that looks like this:
Every time the button is pushed, it calls the pushbutton1_Callback with the hObject, handle of the object that caused the callback, the button, eventdata, in this case it'll be empty, and the handles structure which has a handle to everything in the GUI in this case. Merely do your image processing in the callback function or whatever and it should allow you to get where you're going. HTH!