Loop through data from Listbox

13 ビュー (過去 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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by