MATLAB: Trying to generate function for importing data with the outputs as column vectors. Why is it only creating one variable

column vectordata import

I imported an excel file with 5 columns and 2000+ rows of data. The first column is type Datetime, the other four columns are numbers. I selected column vector as the output type. When I do this manually, it returns the variables I want in the workspace, no issues.
However, when I generate a function for importing the file, the function returns only one variable as "ans" and lists all of the Datetime data in the command window. This code is below. Where is the error and how do I fix it? Thank you.
function [Date,Open,High,Low,Close] = importfile1(workbookFile,sheetName,startRow,endRow)
%IMPORTFILE2 Import data from a spreadsheet
% [Date,Open,High,Low,Close] = IMPORTFILE2(FILE) reads data from the
% first worksheet in the Microsoft Excel spreadsheet file named FILE and
% returns the data as column vectors.

% [Date,Open,High,Low,Close] = IMPORTFILE2(FILE,SHEET) reads from the
% specified worksheet.
% reads from the specified worksheet for the specified row interval(s).
% Specify STARTROW and ENDROW as a pair of scalars or vectors of matching
% size for dis-contiguous row intervals. To read to the end of the file
% specify an ENDROW of inf.%
% Example:
% [Date,Open,High,Low,Close] = importfile2('EURUSD.xlsx','Sheet1',1,2608);
% See also XLSREAD.
% Auto-generated by MATLAB on 2018/05/25 12:25:51
%% 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 = 1; endRow = 2608; end
%% Import the data, extracting spreadsheet dates in Excel serial date format [~, ~, raw, dates] = xlsread(workbookFile, sheetName, sprintf('A%d:E%d',startRow(1),endRow(1)),'' , @convertSpreadsheetExcelDates); for block=2:length(startRow) [~, ~, tmpRawBlock,tmpDateNumBlock] = xlsread(workbookFile, sheetName, sprintf('A%d:E%d',startRow(block),endRow(block)),'' , @convertSpreadsheetExcelDates); raw = [raw;tmpRawBlock]; %#ok<AGROW> dates = [dates;tmpDateNumBlock]; %#ok<AGROW> end raw = raw(:,[2,3,4,5]); dates = dates(:,1);
%% Create output variable I = cellfun(@(x) ischar(x), raw); raw(I) = {NaN}; data = reshape([raw{:}],size(raw));
%% Allocate imported array to column variable names dates(~cellfun(@(x) isnumeric(x) islogical(x), dates)) = {NaN}; Date = datetime([dates{:,1}].', 'ConvertFrom', 'Excel'); Open = data(:,1); High = data(:,2); Low = data(:,3); Close = data(:,4);
% For code requiring serial dates (datenum) instead of datetime, uncomment % the following line(s) below to return the imported dates as datenum(s).
% Date=datenum(Date);

Best Answer

  • You need to specify 5 output arguments to get all the values using this function. For example, run like this in the command window
    [D,O,H,L,C] = importfile1('filename',1,startRow,endRow);