Populating drop down gui from file
조회 수: 4 (최근 30일)
이전 댓글 표시
I created a Load Button which loads the excel file then I want to populate a pop up menu from column E of this file. How can I do that? Currently this give me an error
hload = uicontrol(f,'Style','pushbutton','String','Load Data',...
'Position',[380,260,70,25],...
'Callback',{@loadbutton_Callback});
hsubject = uicontrol(f,'Style','popupmenu',...
'String',{'Injury Level'},...
'Position',[5,260,100,25],...
'Callback',{@subjectpopup_menu_Callback});
%Each callback prompts user to select file to load and display.
function loadbutton_Callback(hObject, eventdata, handles)
disp('Hello 1');
[file] = uigetfile('.xlsx');
disp(file);
[num,txt,raw] = xlsread(file,'E2:E9');
disp(txt);
set(handles.Injury,'String',txt);
guidata(txt,handles.hsubject); %save data to gui
updateTable(txt); %update Table
end
댓글 수: 0
채택된 답변
Image Analyst
2017년 3월 24일
It looks like you're using GUIDE because I see this line:
function loadbutton_Callback(hObject, eventdata, handles)
Yet I also see that somewhere/somehow you're "manually" creating controls using the uicontrol() function. Why are you doing that? Why not just place the things on the GUI using GUIDE? Like the popup menu and the pushbutton??? Assuming you do that, you can simply do
columnE = raw(:, 5);
handles.uitable1.Data = columnE;
or, if you have an antique version of MATLAB, you use the old set() notation:
set(handles.uitable1, 'Data', columnE);
댓글 수: 2
Image Analyst
2017년 3월 24일
Usually/normally you don't mix using GUIDE and doing it yourself manually with uicontrol(). If all that was in the same tutorial, I'd wonder about that tutorial. Was it from the Mathworks, or someone who doesn't work for the Mathworks?
Since you have R2017a you can just do this:
columnE = raw(:, 5); % Where you got raw from using xlsread().
handles.uitable1.Data = columnE; % Use your actual "tag" name of your table rather than uitable1.
추가 답변 (1개)
Geoff Hayes
2017년 3월 24일
Yasaman - you didn't post your error message so we can only guess as to what might be the problem. You mention how you want to populate your popup menu with the data from the Excel file. The relevant code is
[file] = uigetfile('.xlsx');
[num,txt,raw] = xlsread(file,'E2:E9');
set(handles.Injury,'String',txt);
So you are trying to set the handles.Injury with the column E data. But what is this handle? Does it even exist? The popup menu that you create is
hsubject = uicontrol(f,'Style','popupmenu',...
and so the handle is hsubject. If this is the correct popup menu to update, then the code should be
[file] = uigetfile('.xlsx');
[num,txt,raw] = xlsread(file,'E2:E9');
set(hsubject,'String',txt);
You then call
guidata(txt,handles.hsubject); %save data to gui
which I don't understand. What are you attempting with this line? The signature for guidata is either
guidata(object_handle,data)
or
data = guidata(object_handle)
In either case, the first input parameter is an object handle. Usually, I wouldn't use guidata when creating a GUI programmatically so please clarify why you are using it here.
댓글 수: 2
Geoff Hayes
2017년 3월 24일
Did you try making the change that I suggested above? You may need to post all of your code so that we can get a better idea as to what you have written.
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Import from MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!