Loop through data from Listbox

2 次查看(过去 30 天)
I have GUI which loads name of excel files inside listbox. Now I want a loop which can go through each file in the listbox and read the data. directory of files may not be same. xlsread can be used
function pushbuttonLoadfiles_Callback(hObject, eventdata, handles)
[filename,pathname,filterindex] = uigetfile('*.xls;*.xlsx;*.xlsm;*.xlsb', 'Multiselect', 'on');
set(handles.listboxExcelfiles,'string',filename);
function pushbuttonExtract_Callback(hObject, eventdata, handles)
source_files = numel(get(handles.listboxExcelfiles,'value'));
len = length(source_files);
for i = 1 : len
[~ , sheets] = xlsfinfo(source_files(i));
sheetIndex = find(strncmp(sheets, '~', 1));
wanted_sheets = sheets(1, sheetIndex);
set(handles.listboxDatasheets, 'string',wanted_sheets);
end
But here I get this error-
Error using xlsfinfo (line 39)
Filename must be a character vector.
Error in colordefined>pushbuttonExtract_Callback (line 154)
[~ , sheets] = xlsfinfo(source_files(i));
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in colordefined (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)colordefined('pushbuttonExtract_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.

采纳的回答

Walter Roberson
Walter Roberson 2018-2-8
function pushbuttonLoadfiles_Callback(hObject, eventdata, handles)
[filename,pathname,filterindex] = uigetfile('*.xls;*.xlsx;*.xlsm;*.xlsb', 'Multiselect', 'on');
set(handles.listboxExcelfiles, 'string', fullfile(pathname, cellstr(filename)) );
function pushbuttonExtract_Callback(hObject, eventdata, handles)
source_files = numel(get(handles.listboxExcelfiles,'value'));
len = length(source_files);
for i = 1 : len
[~ , sheets] = xlsfinfo(source_files{i});
sheetIndex = find(strncmp(sheets, '~', 1));
wanted_sheets = sheets(1, sheetIndex);
set(handles.listboxDatasheets, 'string',wanted_sheets);
end
The cellstr() compensates for the fact that if the user only selects a single file then 'MultiSelect', 'on' returns a char vector, but if more than one is selected then it returns a cell array of char vector. The cellstr() detects the char vector case and wraps it in a cell.
The fullfile is needed because you need to know the directory to read from, as you indicated that the directory is not always the same.
The change from source_files(i) to source_files{i} is needed for xlsfinfo to not give the message you were seeing.
Note that for every different selected file, you overwrite the listboxDatasheets controls. If you want to support a different sheet selection for each file then you will need to build controls for that, since any one listbox item cannot be a cell array of character vectors in turn.
  4 个评论
Ghenji
Ghenji 2018-2-8
Error using cellstr
Too many output arguments.
Error in colordefined>pushbuttonLoadfiles_Callback (line 82)
[filename,pathname,filterindex] = cellstr(uigetfile('*.xls;*.xlsx;*.xlsm;*.xlsb', 'Multiselect', 'on'));
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in colordefined (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)colordefined('pushbuttonLoadfiles_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
Walter Roberson
Walter Roberson 2018-2-8
I was not paying attention to the numel() call. The line
source_files = numel(get(handles.listboxExcelfiles,'value'));
should be
source_files = get(handles.listboxExcelfiles,'value');

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by