xlsread User defined file

1 visualización (últimos 30 días)
Mazhar
Mazhar el 20 de Sept. de 2013
Hey,
I want to use the xlsread command to read in date from a file. I know the way to use the command is as
data = xlsread('data_file.xlsx','Sheet1');
I want to use it a slightly different way, where I would have a prompt command on the screen asking the user to input the file name and then the sheet name. And it is then these user defined file name and sheet name which are used in the xlsread command.
Is that possible?

Respuestas (1)

Image Analyst
Image Analyst el 20 de Sept. de 2013
Here's one way.
% Have user browse for a file, from a specified "starting folder."
% For convenience in browsing, set a starting folder from which to browse.
startingFolder = 'C:\Program Files\MATLAB';
if ~exist(startingFolder, 'dir')
% If that folder doesn't exist, just start in the current folder.
startingFolder = pwd;
end
% Get the name of the file that the user wants to use.
defaultFileName = fullfile(startingFolder, '*.*');
[baseFileName, folder] = uigetfile(defaultFileName, 'Select a file');
if baseFileName == 0
% User clicked the Cancel button.
return;
end
fullExcelFileName = fullfile(folder, baseFileName)
% Ask user for a sheet name.
defaultValue = 'Sheet1';
titleBar = 'Enter a sheet name';
userPrompt = 'Enter the sheet name ';
caUserInput = inputdlg(userPrompt, titleBar, 1, {num2str(defaultValue)});
if isempty(caUserInput),return,end; % Bail out if they clicked Cancel.
sheetName = strtrim(char(caUserInput))
[num, txt, raw] = xlsread(fullExcelFileName, sheetName);
It's not as robust or user friendly as I nromally have it, though more so than others typically do. For example, it would be far nicer if you used ActiveX to determine what the sheet names are in the workbook and then ask the user to pick one of them via a listbox or the menu() functions. Another thing you need to do is to check for an error if the user types in a sheet name that does not exist and alert the user and take corrective action. In other words, as long as this code is, with three verification "if" sections, you should make it even more robust.
  1 comentario
Mazhar
Mazhar el 20 de Sept. de 2013
Its definitely better than I have right now, where the user has to go in to the script and change it there manually.
Going to give this a go,
Thanks,

Iniciar sesión para comentar.

Categorías

Más información sobre Environment and Settings en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by