showing excel files in listbox(in gui) and opening these excel files from listbox.
2 views (last 30 days)
Show older comments
Muhammad Hussain
on 8 Oct 2014
Commented: Image Analyst
on 9 Oct 2014
Hi,I have made a gui,it has one push button and a listbox.By pushbutton,i am opening a folder in a directory(somewhere in my computer) and then opening subfolders in that folder and renaming a excel file in each subfolder with subfoldername.xlsx and storing it in the parent folder.Now I want to show each excel file(which i selected in subfolders and now it is in parent folder with a new name) in my listbox and from listbox I want to open this excel file.Please tell me how to do it.Thanks
0 Comments
Accepted Answer
Image Analyst
on 8 Oct 2014
Use the system command to launch Excel open to the filename you selected in the listbox. If you don't know how to load a listbox, see this function
% --- Load up the listbox with xls files in folder yourFolder
function handles = LoadListBox(handles)
try
yourFolder = 'C:/whatever';
% Load up the listbox.
ListOfFilenames = {};
dirListing = dir([yourFolder '/*.xls*']);
for Index = 1:length(dirListing)
baseFileName = dirListing(Index).name;
ListOfFilenames = [ListOfFilenames baseFileName];
end
set(handles.Listbox1, 'string', ListOfFilenames);
catch ME
errorMessage = sprintf('Error in LoadListBox().\nThe error reported by MATLAB is:\n\n%s', ME.message);
uiwait(warndlg(errorMessage));
end
return; % from LoadListBox
2 Comments
Image Analyst
on 9 Oct 2014
Try this:
% Get a list of indexes in the listbox that the user has selected.
Selected = get(handles.lstExcelFiles, 'Value');
% If more than one is selected, bail out.
if length(Selected) > 1
return;
end
% If you get to here, exactly 1 is selected.
% Now, get a list of all the items in the listbox.
ListOfFileNames = get(handles.lstExcelFiles, 'string');
% Get the name of the one particular file that has been selected.
baseFileName = strcat(cell2mat(ListOfFileNames(Selected)));
% Get the full file name by prepending the folder where the files live.
excelFullFileName= fullfile(folder, baseFileName);
% Open file in Excel.
winopen(excelFullFileName);
More Answers (0)
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!