Issue with GUI editbox callback
조회 수: 1 (최근 30일)
이전 댓글 표시
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
채택된 답변
Sara
2014년 7월 14일
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
Sara
2014년 7월 14일
In
displacement_buttongroup_SelectionChangeFcn
typedata_buttongroup_SelectionChangeFcn
move guidata(hObject, handles); after the switch end.
추가 답변 (1개)
Ben11
2014년 7월 14일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Maintain or Transition figure-Based Apps에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!