can u pass sheetnames(Not numbers please) of excel file using matlab so it reads only that particular sheet from the excel file and not anything else?
MATLAB: Reading excel sheetname n extracting data from that using matlab
excel read
Related Solutions
You can rename an Excel sheet using the "Name" property of a sheet object. For example the following renames the first sheet of an Excel file:
filename = 'C:\SomeExcelFile.xls';% Open Excel Automation server
Excel = actxserver('Excel.Application');% Make Excel visible
Excel.Visible=1;% Open Excel file
Workbook = Excel.Workbooks.Open(filename);% Get the list of sheets in the workbook
Sheets = Excel.ActiveWorkbook.Sheets;% Rename the first sheet
Sheets.Item(1).Name = 'This is sheet 1';
The following code then saves the Workbook, quits Excel and removes the COM server:
% Save the file
Workbook.Save();% Quit Excel, remove the COM server and delete the related objects
Excel.Quit();Excel.delete();clear Excel;clear Workbook;clear Sheets;
Assuming that filepath is just a list of folders (no filename) and filelist is the corresponding list of files (just filenames), then the simplest thing might be to concatenate the two into just one list (with fullfile) and just pass that list to the check function. Therefore,
in the calling code:
fullpaths = fullfile(filepath, filelist); %concatenate paths with filenames
hassheet = CheckForWorksheet(fullpaths, 'Cooking_is_fun');%fullpaths(hassheet) is what you want to keep
the check function:
function hassheet = CheckExcelFilesForWorksheet(filelist, sheetname) %HASSHEET Check whether or not the given excel files have a sheet with the given name
%filelist: full paths of excel files (cell array of 1D char arrays / string array)
%sheetname: name of sheet to find in excel files (1D char array / string)
%hassheet: array the same size as filelist, indicating whether or not the excel file has a sheet with sheetname (logical)
hassheet = false(size(filelist)); %output
excel = actxserver('Excel.Application'); %start microsoft excel
cleanupobj = onCleanup(@() excel.Quit); %close excel when function returns or error occurs
for fileidx = 1:numel(filelist) %open without updating link and as read only. read only ensure the file can be opened even if it's already in use:
workbook = excel.Workbooks.Open(filelist{fileidx}, false, true); %get the list of worksheet name by iterating over the Sheets collection:
sheetnames = arrayfun(@(i) workbook.Sheets.Item(i).Name, 1:workbook.Sheets.Count, 'UniformOutput', false); workbook.Close(false); %close workbook, without saving
if ismember(sheetname, sheetnames) hassheet(fileidx) = true; end end end
Note that you should really learn what a cell array is. Yes, your filelist and filepath are cell arrays. Cell arrays are containers like matrices except that matrices can only contain numbers while cell arrays can contain anything. Therefore, if you want to put a bunch of strings (char arrays) together, you put them in a cell array.
Best Answer