plot excel file from listbox selection using GUIDE GUI
7 ビュー (過去 30 日間)
古いコメントを表示
Hi,
I am trying to create a GUI using guide that has 1) a push button that loads multiple files into a 2) listbox, from which 3) you can select one or (ideally multiple) files to display on a plot (see screencap).

currently this code populates the listbox using the pushbutton (successful)
% --- Executes on button press in pushbuttonLoadXLS.
function pushbuttonLoadXLS_Callback(hObject, eventdata, handles)
% hObject handle to pushbuttonLoadXLS (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%GOALS: get file, set popup menus to reflect column names, update axes when
%popup selection is updated
% LOAD MULTIPLE FILES
[filename,pathname] = uigetfile('*.xlsx', 'Chose files to load:','MultiSelect','on');
nfiles = length(filename);
for k = 1:nfiles
file = fullfile(filename,pathname(k));
end
%update master copy of handles information
guidata(hObject, handles);
% update listbox with the result from above
set(handles.listbox2, 'String', fullfile(filename,pathname));
%set popup menus and update axes callbacks for dropdowns and radiobuttons
setPopupmenuString(handles.popupmenuX,eventdata,handles)
setPopupmenuString(handles.popupmenuY,eventdata,handles)
set(handles.popupmenuX, 'callback', 'HarkerGuiMulti(''updateAxes'',gcbo, [], guidata(gcbo))');
set(handles.popupmenuY, 'callback', 'HarkerGuiMulti(''updateAxes'',gcbo, [], guidata(gcbo))');
but this code does not work for reading an excel file in when it is selected in the listbox,
% --- Executes on selection change in listbox2.
function listbox2_Callback(hObject, eventdata, handles)
% hObject handle to listbox2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns listbox2 contents as cell array
% contents{get(hObject,'Value')} returns selected item from listbox2
%SELECT FILE FROM LISTBOX
% get(handles.figure1,'SelectionType')
% if strcmp(get(handles.figure1,'SelectionType'),'open')
index_selected = get(handles.listbox2,'Value');
file_list = get(handles.listbox2,'string');
selected = file_list{index_selected};
handles.data = xlsread(fullfile(selected));
the above code gives me the following error
Error using xlsread (line 139)
XLSREAD unable to open file
'HAB20_liquid.xlsx\C:\Users\emily\Documents\UW
Madison Coursework\GEO537 Quant Methods
Matlab\project\test_data\'.
File
'HAB20_liquid.xlsx\C:\Users\emily\Documents\UW
Madison Coursework\GEO537 Quant Methods
Matlab\project\test_data\' not found.
Error in HarkerGuiMulti>listbox2_Callback (line
200)
handles.data = xlsread(fullfile(selected));
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in HarkerGuiMulti (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)HarkerGuiMulti('listbox2_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
any help is much appreciated!
0 件のコメント
回答 (1 件)
Cris LaPierre
2018 年 12 月 12 日
編集済み: Cris LaPierre
2018 年 12 月 12 日
You do populate the listbox, but your filenames are messed up (the filename comes first followed by the path name)
'HAB20_liquid.xlsx\C:\Users\emily\Documents\UW Madison Coursework\GEO537 Quant Methods Matlab\project\test_data\'
If you read the error message, you can see that you have placed the path information in the wrong place.
Error using xlsread (line 139)
XLSREAD unable to open file
'HAB20_liquid.xlsx\C:\Users\emily\Documents\UW Madison Coursework\GEO537 Quant Methods Matlab\project\test_data\'
Your pushbutton code contains the following code:
for k = 1:nfiles
file = fullfile(filename,pathname(k));
end
Note that fullfile puts things together in the order you place them, while uigetfile returns filename first, then pathname.
fullfile('part1','part2','part3')
ans =
'part1\part2\part3'
2 件のコメント
Cris LaPierre
2018 年 12 月 13 日
編集済み: Cris LaPierre
2018 年 12 月 13 日
Ah yes. One more little trick to show you about guide. Remember, callbacks are functions, and variables created inside a function do not exist once the funcion has terminated (unless they are passed out). Each callback has handles as an input, but no outputs...
Guide has a little trick - saving the handles structure to the figure window itself. Yeah, don't worry about the details. What you need to do is, at the bottom of your function, if you've modified the handles structure, store the updated handles structure to the figure using guidata.
function listbox2_Callback(hObject, eventdata, handles)
...
file_list = get(handles.listbox2,'string');
selected = file_list{index_selected};
handles.data = xlsread(fullfile(selected));
% Update handles structure
guidata(hObject, handles);
参考
カテゴリ
Help Center および File Exchange で Printing and Saving についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!