Clear Filters
Clear Filters

Issue with GUI editbox callback

1 view (last 30 days)
Zeke Merchant
Zeke Merchant on 14 Jul 2014
Commented: Sara on 14 Jul 2014
I need to save the text entered into two edit boxes so that I can concatenate them with other strings to create the name of a desired file. Example code below:
function subject_editbox_Callback(hObject, eventdata, handles)
% hObject handle to subject_editbox (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
input=get(hObject,'String');
display(input)
sub=input;
function trial_editbox_Callback(hObject, eventdata, handles)
% hObject handle to trial_editbox (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
input = get(hObject,'String');
display(input)
trial = num2str(input);
% --- Executes on button press in createfile.
function createfile_Callback(hObject, eventdata, handles)
% hObject handle to createfile (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
Note: variables "displacement and "file_type" specified in individual button group SelectionChangeFcn callbacks
filed=strcat(sub,displacement);
filedesi=strcat(filed,trial);
filedesired=strcat(filedesi, file_type)
display(filedesired)
When I click on the pushbutton, it says:
"Undefined function or variable 'sub'.
Error in Vicon_GUI_Zeke>createfile_Callback (line 225) filed=strcat(sub,displacement);
Error in gui_mainfcn (line 95) feval(varargin{:});
Error in Vicon_GUI_Zeke (line 42) gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)Vicon_GUI_Zeke('createfile_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback"

Accepted Answer

Sara
Sara on 14 Jul 2014
You need to save your variables (i.e. the strings from the editboxes) as fields of the structure handles. So correct your functions as the one below:
function subject_editbox_Callback(hObject, eventdata, handles)
input=get(hObject,'String');
display(input)
handles.sub=input;
guidata(hObject, handles);
Only then you can concatenate handles.sub with handles.displacement
  6 Comments
Zeke Merchant
Zeke Merchant on 14 Jul 2014
function varargout = Vicon_GUI_Zeke(varargin) % VICON_GUI_ZEKE M-file for Vicon_GUI_Zeke.fig % VICON_GUI_ZEKE, by itself, creates a new VICON_GUI_ZEKE or raises the existing % singleton*. % % H = VICON_GUI_ZEKE returns the handle to a new VICON_GUI_ZEKE or the handle to % the existing singleton*. % % VICON_GUI_ZEKE('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in VICON_GUI_ZEKE.M with the given input arguments. % % VICON_GUI_ZEKE('Property','Value',...) creates a new VICON_GUI_ZEKE or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before Vicon_GUI_Zeke_OpeningFunction gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to Vicon_GUI_Zeke_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 Vicon_GUI_Zeke
% Last Modified by GUIDE v2.5 14-Jul-2014 13:30:31
% Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @Vicon_GUI_Zeke_OpeningFcn, ... 'gui_OutputFcn', @Vicon_GUI_Zeke_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 Vicon_GUI_Zeke is made visible. function Vicon_GUI_Zeke_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 Vicon_GUI_Zeke (see VARARGIN)
% Choose default command line output for Vicon_GUI_Zeke handles.output = hObject;
% Update handles structure guidata(hObject, handles);
% UIWAIT makes Vicon_GUI_Zeke wait for user response (see UIRESUME) % uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line. function varargout = Vicon_GUI_Zeke_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 subject_editbox_Callback(hObject, eventdata, handles)
% hObject handle to subject_editbox (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
input=get(hObject,'String');
display(input)
handles.sub=input
guidata(hObject, handles);
% Hints: get(hObject,'String') returns contents of subject_editbox as text
% str2double(get(hObject,'String')) returns contents of
subject_editbox as a double
% --- Executes during object creation, after setting all properties.
function subject_editbox_CreateFcn(hObject, eventdata, handles)
% hObject handle to subject_editbox (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 radiobutton1.
function radiobutton1_Callback(hObject, eventdata, handles)
% hObject handle to radiobutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of radiobutton1
% --- Executes on button press in radiobutton4.
function radiobutton4_Callback(hObject, eventdata, handles)
% hObject handle to radiobutton4 (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 radiobutton4
% --- Executes on button press in radiobutton16.
function radiobutton16_Callback(hObject, eventdata, handles)
% hObject handle to radiobutton16 (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 radiobutton16
% --- Executes on button press in radiobutton3LD.
function radiobutton3LD_Callback(hObject, eventdata, handles)
% hObject handle to radiobutton3LD (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 radiobutton3LD
% --- Executes on button press in radiobutton3LA.
function radiobutton3LA_Callback(hObject, eventdata, handles)
% hObject handle to radiobutton3LA (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 radiobutton3LA
% --- Executes on button press in radiobutton3LV.
function radiobutton3LV_Callback(hObject, eventdata, handles)
% hObject handle to radiobutton3LV (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 radiobutton3LV
function trial_editbox_Callback(hObject, eventdata, handles)
% hObject handle to trial_editbox (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
input = get(hObject,'String');
display(input)
handles.trial=input;
guidata(hObject, handles);
% Hints: get(hObject,'String') returns contents of trial_editbox as text
% str2double(get(hObject,'String')) returns contents of
trial_editbox as a double
% --- Executes during object creation, after setting all properties.
function trial_editbox_CreateFcn(hObject, eventdata, handles)
% hObject handle to trial_editbox (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 when selected object is changed in displacement_buttongroup.
function displacement_buttongroup_SelectionChangeFcn(hObject, eventdata, handles)
% hObject handle to the selected object in displacement_buttongroup
% eventdata structure with the following fields (see UIBUTTONGROUP)
% EventName: string 'SelectionChanged' (read only)
% OldValue: handle of the previously selected object or empty if none was selected
% NewValue: handle of the currently selected object
% handles structure with handles and user data (see GUIDATA)
switch get(eventdata.NewValue,'Tag') % Get Tag of selected object.
case 'radiobutton1'
handles.displacement='afc01c1asfh_';
case 'radiobutton4'
handles.displacement='afc04c1asfh_';
case 'radiobutton16'
handles.displacement='afc16c1asfh_';
guidata(hObject, handles);
end
% --- Executes when selected object is changed in typedata_buttongroup.
function typedata_buttongroup_SelectionChangeFcn(hObject, eventdata, handles)
% hObject handle to the selected object in typedata_buttongroup
% eventdata structure with the following fields (see UIBUTTONGROUP)
% EventName: string 'SelectionChanged' (read only)
% OldValue: handle of the previously selected object or empty if none was selected
% NewValue: handle of the currently selected object
% handles structure with handles and user data (see GUIDATA)
switch get(eventdata.NewValue,'Tag') % Get Tag of selected object.
case 'radiobutton3LD'
handles.file_type='.3LD';
case 'radiobutton_3LV'
handles.file_type='.3LV';
case 'radiobutton3LA'
handles.file_type='.3LA';
guidata(hObject, handles);
end
% --- Executes on button press in createfile.
function createfile_Callback(hObject, eventdata, handles)
% hObject handle to createfile (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles = guidata(hObject);
filed=strcat(handles.sub, handles.displacement);
filedesi=strcat(filed, handles.trial);
filedesired=strcat(filedesi, handles.file_type)
display(filedesired)
guidata(hObject,handles)
Sara
Sara on 14 Jul 2014
In
displacement_buttongroup_SelectionChangeFcn
typedata_buttongroup_SelectionChangeFcn
move guidata(hObject, handles); after the switch end.

Sign in to comment.

More Answers (1)

Ben11
Ben11 on 14 Jul 2014
You need to "share" the data between the different callbacks of your GUI, otherwise each callback does not know what is in other callbacks.
As a solution you could store both names you wish to concatenate in the GUI handles structure and get them in any callback you want. For example you could have:
function subject_editbox_Callback(hObject, eventdata, handles)
% hObject handle to subject_editbox (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
input=get(hObject,'String');
display(input)
handles.sub=input;
guidata(hObject,handles); % IMPORTANT, keeps the handles structure updated
where handles.sub contains the name you want to use in other callbacks. Then in those callbacks, you retrieve the structure with this:
handles = guidata(hObject)
...insert your code here
guidata(hObject,handles)
Hope it's clear enough :) If not please ask for more details.

Categories

Find more on Maintain or Transition figure-Based Apps in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!