GUI: Can't access data from another callback function with handles

조회 수: 2 (최근 30일)
Peter Smith
Peter Smith 2019년 8월 30일
댓글: Peter Smith 2019년 9월 3일
I'm using GUIDE, and I've imported data like this:
function control_browse_button_Callback(hObject, eventdata, handles)
% hObject handle to control_browse_button (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 control_browse_button
[control_file control_pathname] = uigetfile({'*.xlsx'}, 'File Selector');
set(handles.control_filename,'String', control_file);
[control_data control_labels] = xlsread(control_file);
handles.control_data = control_data;
handles.control_labels = control_labels;
And then I attempted to access control_labels:
function loadfile_button_Callback(hObject, eventdata, handles)
% hObject handle to loadfile_button (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 loadfile_button
for i = 3:size(handles.control_labels,1)
ctrl_labels{i-2} = handles.control_labels{i,1};
end
....
%(rest of the code for this callback)
...
But I'm getting the error:
Reference to non-existent field 'control_labels'
Am I not using handles properly? How should I access control_labels from another callback function?
Thanks!
  댓글 수: 1
Rik
Rik 2019년 8월 31일
It's also a good idea to initialize the field when starting the GUI, and trigger an error if the user clicks the load button before the control browse button.
@G A: please move your answer to the answer section.

댓글을 달려면 로그인하십시오.

채택된 답변

G A
G A 2019년 9월 1일
G A on 31 Aug 2019 at 8:38
Put the line
guidata(hObject, handles);
at the end of your function control_browse_button_Callback.
Have a look here:
doc guidata

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Specifying Target for Graphics Output에 대해 자세히 알아보기

제품


릴리스

R2013b

Community Treasure Hunt

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

Start Hunting!

Translated by