필터 지우기
필터 지우기

xlsread User defined file

조회 수: 2 (최근 30일)
Mazhar
Mazhar 2013년 9월 20일
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?

답변 (1개)

Image Analyst
Image Analyst 2013년 9월 20일
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
Mazhar
Mazhar 2013년 9월 20일
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,

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Develop Apps Using App Designer에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by