Loop through data from Listbox

조회 수: 3 (최근 30일)
Ghenji
Ghenji 2018년 2월 8일
댓글: Walter Roberson 2018년 2월 8일
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