Importing Excel Files using a Function

4 Ansichten (letzte 30 Tage)
Matthew Zelko
Matthew Zelko am 14 Aug. 2015
Kommentiert: Matthew Zelko am 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;
end
% If row start and end points are not specified, define defaults
if nargin <= 3
startRow = 2;
endRow = 9472;
end
%%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>
end
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?
Matt

Antworten (2)

Muthu Annamalai
Muthu Annamalai am 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 http://www.mathworks.com/help/matlab/ref/narginchk.
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 Kommentar
Matthew Zelko
Matthew Zelko am 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,
Matt

Melden Sie sich an, um zu kommentieren.


Walter Roberson
Walter Roberson am 14 Aug. 2015
If you do not wish to change the defaults then at the command window just invoke
ExcelImport
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 Kommentar
Matthew Zelko
Matthew Zelko am 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?
Thanks,
Matt

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by