Populating drop down gui from file

5 Ansichten (letzte 30 Tage)
Yasaman Best
Yasaman Best am 23 Mär. 2017
Kommentiert: Image Analyst am 24 Mär. 2017
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

Akzeptierte Antwort

Image Analyst
Image Analyst am 24 Mär. 2017
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 Kommentare
Yasaman Best
Yasaman Best am 24 Mär. 2017
Ha ! No i have matlab 2017 but did that based on an online tutorial ! I'm gonna try it now
Image Analyst
Image Analyst am 24 Mär. 2017
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Geoff Hayes
Geoff Hayes am 24 Mär. 2017
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 Kommentare
Yasaman Best
Yasaman Best am 24 Mär. 2017
Thank you for your reply ! Honestly I'm so confused . All I wanna to is assign values to pop up drop down menu .., I'm guessing using handle ?
Geoff Hayes
Geoff Hayes am 24 Mär. 2017
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Import from MATLAB finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by