MATLAB Answers

Importing Excel Files using a Function

조회 수: 26(최근 30일)
Matthew Zelko
Matthew Zelko 14 Aug 2015
댓글: Matthew Zelko 14 Aug 2015
Hi there,
Relatively new to Matlab, so bear with me. I am trying to import Excel files using the Uigetfile function embedded in the Importfile function created when I choose to generate function in the Import File Wizard.
Currently my code is as follows:
function datasetout = importfile(workbookFile,sheetName,startRow,endRow)
[Filename, Pathname] = uigetfile(['*.xlsx']);
workbookFile = fullfile(Pathname, Filename);
assert(exist(workbookFile,'file')==2,'%s doesnt not exist.', workbookFile);
%%Input handling
% If no sheet is specified, read first sheet
if nargin == 1 || isempty(sheetName)
sheetName = 1;
% If row start and end points are not specified, define defaults
if nargin <= 3
startRow = 2;
endRow = 9472;
%%Import the data
[~, ~, raw] = xlsread(workbookFile, sheetName, sprintf('A%d:C%d',startRow(1),endRow(1)));
for block=2:length(startRow)
[~, ~, tmpRawBlock] = xlsread(workbookFile, sheetName, sprintf('A%d:C%d',startRow(block),endRow(block)));
raw = [raw;tmpRawBlock]; %#ok<AGROW>
raw(cellfun(@(x) ~isempty(x) && isnumeric(x) && isnan(x),raw)) = {''};
%%Replace non-numeric cells with NaN
R = cellfun(@(x) ~isnumeric(x) && ~islogical(x),raw); % Find non-numeric cells
raw(R) = {NaN}; % Replace non-numeric cells
%%Create output variable
data = reshape([raw{:}],size(raw));
%%Create dataset array
datasetout = dataset;
%%Allocate imported array to column variable names
datasetout.BPM = data(:,1);
datasetout.Time = data(:,2);
datasetout.IBI = data(:,3);
The Command Window returns the Error
ExcelImport(workbookFile, sheetName, startRow, endRow)
Undefined function or variable 'workbookFile'.
I've found if I manually enter the variables into the workspace, it works but this negates the Uigetfile capacity of the function. What am I doing wrong?

  댓글 수: 0

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


Muthu Annamalai
Muthu Annamalai 14 Aug 2015
MATLAB is not able to find your variable workbookFile.
You should try to add a call to narginchk(1,3) in your code, like in
Also you may want to change the function argument workbookFile to workbookFile_in or something more sensible.
One reason why this may happen, workbookFile is not being defined is when your uigetfile is popupdialog is dismissed. To guard against that wrap your uigetfile call in a try-catch and assign a sensible default to workbookFile variable.
If variable does not exist error out.

  댓글 수: 1

Matthew Zelko
Matthew Zelko 14 Aug 2015
Hi Muthu,
That link doesn't exist unfortunately. How would I "wrap" my uigetfile call in a try-catch and assign a sensible default to workbookFile variable?
Thanks again for your input,

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

Walter Roberson
Walter Roberson 14 Aug 2015
If you do not wish to change the defaults then at the command window just invoke
If you do wish to change the defaults, you can pass in anything for the first parameter, such as
ExcelImport([], [], 5, 1949)
where the first parameter will be ignored, the second indicates default sheet, and the 5 and 1949 establish start and end rows.

  댓글 수: 1

Matthew Zelko
Matthew Zelko 14 Aug 2015
Hi Walter.
Thanks for your input here. I am unsure how to adapt my code as you have suggested. Are you suggesting to just use Excel Import in the Command Window instead of using my function?

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

Community Treasure Hunt

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

Start Hunting!

Translated by