Code covered by the BSD License  

Highlights from
Machine Learning with MATLAB

image thumbnail

Machine Learning with MATLAB


Abhishek Gupta (view profile)


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.
%   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;

%% 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)
    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}];

%% Close the text file.

%% 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