Best/Cleanest way to select a specific sheet from an excel file?
I am importing an excel file that has multiple sheets. I am using the readtable command to import the file. For the sheet choice, I am giving the read table command the ‘Sheet’ modifier with the sheet name.
My question is about getting the sheet name. Currently, I have the "sheetnames" command get a string array of the sheet names, then I use "listdlg" to display them and return a selection. the listdlg command is just not the best though. The formatting does not look very good, and I have multiple monitors. It does not pop up on the same monitor or the previously used monitor. It always pops up on the first, which if you need to run the program many times quickly, means you have to move across all monitors to select an option. Is there a better way to format it or an alternative to the "listdlg"?
Is there a different way of doing all of this that is better than sheetnames and listdlg? My program I’ve written pretty heavily relies on "readtable", so I cannot change that aspect.
% get the file name and file path of the selected excel file
[fileName,pathName] = uigetfile(‘*xlsx;*.xls’,’Select the Excel file’);
% check if user canceled file selection and act accordingly
if fileName == 0
return;
end
% combine the file name and file path to get the complete file path of the
% selected excel file
completeTableFilePath = fullfile(pathName,fileName);
% gather the names of all the sheets of the excel file
sheetNamesArray = sheetnames(completeTableFilePath);
% create a dialog box that lists all the sheet names in the file and
% prompts the user to pick one
sheetChoice = listdlg(‘PromptString’,’Select the sheet:’,’SelectionMode’,’single’,’ListString’,sheetNamesArray);
% check if user canceled file selection
if isempty(sheetChoice)
return;
end
% remembers the index of the selected excel sheet
sheetChoiceFileName = sheetNamesArray(sheetChoice);
% intake the specific sheet of the selected excel file as strings
inputTable = readtable(completeTableFilePath,’Sheet’,sheetChoiceFileName,’TextType’,’string’);I am importing an excel file that has multiple sheets. I am using the readtable command to import the file. For the sheet choice, I am giving the read table command the ‘Sheet’ modifier with the sheet name.
My question is about getting the sheet name. Currently, I have the "sheetnames" command get a string array of the sheet names, then I use "listdlg" to display them and return a selection. the listdlg command is just not the best though. The formatting does not look very good, and I have multiple monitors. It does not pop up on the same monitor or the previously used monitor. It always pops up on the first, which if you need to run the program many times quickly, means you have to move across all monitors to select an option. Is there a better way to format it or an alternative to the "listdlg"?
Is there a different way of doing all of this that is better than sheetnames and listdlg? My program I’ve written pretty heavily relies on "readtable", so I cannot change that aspect.
% get the file name and file path of the selected excel file
[fileName,pathName] = uigetfile(‘*xlsx;*.xls’,’Select the Excel file’);
% check if user canceled file selection and act accordingly
if fileName == 0
return;
end
% combine the file name and file path to get the complete file path of the
% selected excel file
completeTableFilePath = fullfile(pathName,fileName);
% gather the names of all the sheets of the excel file
sheetNamesArray = sheetnames(completeTableFilePath);
% create a dialog box that lists all the sheet names in the file and
% prompts the user to pick one
sheetChoice = listdlg(‘PromptString’,’Select the sheet:’,’SelectionMode’,’single’,’ListString’,sheetNamesArray);
% check if user canceled file selection
if isempty(sheetChoice)
return;
end
% remembers the index of the selected excel sheet
sheetChoiceFileName = sheetNamesArray(sheetChoice);
% intake the specific sheet of the selected excel file as strings
inputTable = readtable(completeTableFilePath,’Sheet’,sheetChoiceFileName,’TextType’,’string’); I am importing an excel file that has multiple sheets. I am using the readtable command to import the file. For the sheet choice, I am giving the read table command the ‘Sheet’ modifier with the sheet name.
My question is about getting the sheet name. Currently, I have the "sheetnames" command get a string array of the sheet names, then I use "listdlg" to display them and return a selection. the listdlg command is just not the best though. The formatting does not look very good, and I have multiple monitors. It does not pop up on the same monitor or the previously used monitor. It always pops up on the first, which if you need to run the program many times quickly, means you have to move across all monitors to select an option. Is there a better way to format it or an alternative to the "listdlg"?
Is there a different way of doing all of this that is better than sheetnames and listdlg? My program I’ve written pretty heavily relies on "readtable", so I cannot change that aspect.
% get the file name and file path of the selected excel file
[fileName,pathName] = uigetfile(‘*xlsx;*.xls’,’Select the Excel file’);
% check if user canceled file selection and act accordingly
if fileName == 0
return;
end
% combine the file name and file path to get the complete file path of the
% selected excel file
completeTableFilePath = fullfile(pathName,fileName);
% gather the names of all the sheets of the excel file
sheetNamesArray = sheetnames(completeTableFilePath);
% create a dialog box that lists all the sheet names in the file and
% prompts the user to pick one
sheetChoice = listdlg(‘PromptString’,’Select the sheet:’,’SelectionMode’,’single’,’ListString’,sheetNamesArray);
% check if user canceled file selection
if isempty(sheetChoice)
return;
end
% remembers the index of the selected excel sheet
sheetChoiceFileName = sheetNamesArray(sheetChoice);
% intake the specific sheet of the selected excel file as strings
inputTable = readtable(completeTableFilePath,’Sheet’,sheetChoiceFileName,’TextType’,’string’); listdlg, importing excel data, performance, gui MATLAB Answers — New Questions