Loop through data from Listbox
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Ghenji
il 8 Feb 2018
Commentato: Walter Roberson
il 8 Feb 2018
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.
0 Commenti
Risposta accettata
Walter Roberson
il 8 Feb 2018
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 Commenti
Walter Roberson
il 8 Feb 2018
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');
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Migrate GUIDE Apps in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!