Issue with GUI editbox callback
1 view (last 30 days)
Show older comments
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"
0 Comments
Accepted Answer
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
Sara
on 14 Jul 2014
In
displacement_buttongroup_SelectionChangeFcn
typedata_buttongroup_SelectionChangeFcn
move guidata(hObject, handles); after the switch end.
More Answers (1)
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.
0 Comments
See Also
Categories
Find more on Maintain or Transition figure-Based Apps in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!