-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimportfile.m
59 lines (50 loc) · 2.08 KB
/
importfile.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
function irisdata = importfile(filename, startRow, endRow)
%IMPORTFILE Import numeric data from a text file as a matrix.
% IRISDATA = IMPORTFILE(FILENAME) Reads data from text file FILENAME for
% the default selection.
%
% IRISDATA = IMPORTFILE(FILENAME, STARTROW, ENDROW) Reads data from rows
% STARTROW through ENDROW of text file FILENAME.
%
% Example:
% irisdata = importfile('irisdata.csv', 2, 151);
%
% See also TEXTSCAN.
% Auto-generated by MATLAB on 2017/12/12 20:04:17
%% Initialize variables.
delimiter = ',';
if nargin<=2
startRow = 2;
endRow = inf;
end
%% Format for each line of text:
% column1: double (%f)
% column2: double (%f)
% column3: double (%f)
% column4: double (%f)
% column5: categorical (%C)
% For more information, see the TEXTSCAN documentation.
formatSpec = '%f%f%f%f%C%[^\n\r]';
%% Open the text file.
fileID = fopen(filename,'r');
%% Read columns of data according to the format.
% 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, 'TextType', 'string', 'HeaderLines', startRow(1)-1, 'ReturnOnError', false, 'EndOfLine', '\r\n');
for block=2:length(startRow)
frewind(fileID);
dataArrayBlock = textscan(fileID, formatSpec, endRow(block)-startRow(block)+1, 'Delimiter', delimiter, 'TextType', 'string', 'HeaderLines', startRow(block)-1, 'ReturnOnError', false, 'EndOfLine', '\r\n');
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
irisdata = table(dataArray{1:end-1}, 'VariableNames', {'sepal_length','sepal_width','petal_length','petal_width','species'});