Hi, I am trying to filter out excel files by checking whether it has a sheet named "Cooking_is_fun" within the excel.
This is the code I have been working on.
function hassheet = CheckForWorksheet(filepath, filelist, sheetname) hassheet = false(size(filelist)); excel = actxserver('Excel.Application'); cleanupobj = onCleanup(@() excel.Quit); for fileidx = 1:numel(filelist) workbook = excel.Workbooks.Open(filepath{fileidx}, false, true); sheetnames = arrayfun(@(i) workbook.Sheets.Item(i).Name, 1:workbook.Sheets.Count, 'UniformOutput', false); if ismember(sheetname, sheetnames) hassheet(fileidx) = true; end endend %now call the function, note that data is a pre-existing structure array that I have.
filepath = {data.AllExcelPath};filelist = {data.AllExcel};hassheet = CheckForWorksheet(SubFolder, filelist, 'Cooking_is_fun');wantedExcel = filelist(hassheet);
However, this kept on giving me errors such as:
Cell contents reference from a non-cell array object.Error in Open_Excel_Files_All/CheckForWorksheet workbook = excel.Workbooks.Open(filepath{fileidx}, false, true);Error in Open_Excel_Files_All hassheet = CheckForWorksheet(SubFolder, filelist,'Cooking_is_fun');
My goal is to:
be able to write the function and call the function in the same script, so that when I send the code to someone they don't have to run a function and a m-file separately.
be able to extract wanted excel files based on whether it contain a sheet named "Cooking_is_fun".
I am fairly new to matlab and am still confused with many basic concepts. Please advice me with any tips/guide.
Thank you very much for reading and any help will be greatly appreciated 🙂
Best Answer