Code covered by the BSD License  

Highlights from
Machine Learning with MATLAB

image thumbnail

Machine Learning with MATLAB

by

 

05 Aug 2013 (Updated )

These are the supporting MATLAB files for the MathWorks webinar of the same name.

ImportBankData(filename, startRow, endRow)
function bank1 = ImportBankData(filename, startRow, endRow)
%IMPORTFILE Import numeric data from a text file as a matrix.
%   BANK1 = IMPORTFILE(FILENAME) Reads data from text file FILENAME for the
%   default selection.
%
%   BANK1 = IMPORTFILE(FILENAME, STARTROW, ENDROW) Reads data from rows
%   STARTROW through ENDROW of text file FILENAME.
%
% Example:
%   bank1 = importfile('bank.csv', 2, 4522);
%
%    See also TEXTSCAN.

% Auto-generated by MATLAB on 2013/05/13 11:32:20

% Copyright 2013 The MathWorks, Inc.

%% Initialize variables.
delimiter = ';';
if nargin<=2
    startRow = 2;
    endRow = inf;
end

%% Format string for each line of text:
%   column1: double (%f)
%	column2: text (%s)
%   column3: text (%s)
%	column4: text (%s)
%   column5: text (%s)
%	column6: double (%f)
%   column7: text (%s)
%	column8: text (%s)
%   column9: text (%s)
%	column10: double (%f)
%   column11: text (%s)
%	column12: double (%f)
%   column13: double (%f)
%	column14: double (%f)
%   column15: double (%f)
%	column16: text (%s)
%   column17: text (%s)
% For more information, see the TEXTSCAN documentation.
formatSpec = '%f%s%s%s%s%f%s%s%s%f%s%f%f%f%f%s%s%[^\n\r]';

%% Open the text file.
fileID = fopen(filename,'r');

%% Read columns of data according to format string.
% This call is based on the structure of the file used to generate this
% code. If an error occurs for a different file, try regenerating the code
% from the Import Tool.
dataArray = textscan(fileID, formatSpec, endRow(1)-startRow(1)+1, 'Delimiter', delimiter, 'EmptyValue' ,NaN,'HeaderLines', startRow(1)-1, 'ReturnOnError', false);
for block=2:length(startRow)
    frewind(fileID);
    dataArrayBlock = textscan(fileID, formatSpec, endRow(block)-startRow(block)+1, 'Delimiter', delimiter, 'EmptyValue' ,NaN,'HeaderLines', startRow(block)-1, 'ReturnOnError', false);
    for col=1:length(dataArray)
        dataArray{col} = [dataArray{col};dataArrayBlock{col}];
    end
end

%% Close the text file.
fclose(fileID);

%% Post processing for unimportable data.
% No unimportable data rules were applied during the import, so no post
% processing code is included. To generate code which works for
% unimportable data, select unimportable cells in a file and regenerate the
% script.

%% Create output variable
bank1 = dataset(dataArray{1:end-1}, 'VarNames', {'age','job','marital','education','default','balance','housing','loan','contact','day','month','duration','campaign','pdays','previous','poutcome','y'});

Contact us