IMPORTFILE Import numeric data from a text file as column vectors. [VARNAME1,VARNAME2] = IMPORTFILE(FILENAME) Reads data from text file FILENAME for the default selection. [VARNAME1,VARNAME2] = IMPORTFILE(FILENAME, STARTROW, ENDROW) Reads data from rows STARTROW through ENDROW of text file FILENAME. Example: [VarName1,VarName2] = importfile('m42_rasters.txt',1, 12); See also TEXTSCAN.
0001 function [VarName1,VarName2] = import_raster_file(filename, startRow, endRow) 0002 %IMPORTFILE Import numeric data from a text file as column vectors. 0003 % [VARNAME1,VARNAME2] = IMPORTFILE(FILENAME) Reads data from text file 0004 % FILENAME for the default selection. 0005 % 0006 % [VARNAME1,VARNAME2] = IMPORTFILE(FILENAME, STARTROW, ENDROW) Reads data 0007 % from rows STARTROW through ENDROW of text file FILENAME. 0008 % 0009 % Example: 0010 % [VarName1,VarName2] = importfile('m42_rasters.txt',1, 12); 0011 % 0012 % See also TEXTSCAN. 0013 0014 % Auto-generated by MATLAB on 2013/08/12 14:30:31 0015 0016 %% Initialize variables. 0017 delimiter = ' '; 0018 if nargin<=2 0019 startRow = 1; 0020 endRow = inf; 0021 end 0022 0023 %% Format string for each line of text: 0024 % column1: text (%s) 0025 % column2: text (%s) 0026 % For more information, see the TEXTSCAN documentation. 0027 formatSpec = '%s%s%*s%[^\n\r]'; 0028 0029 %% Open the text file. 0030 fileID = fopen(filename,'r'); 0031 0032 %% Read columns of data according to format string. 0033 % This call is based on the structure of the file used to generate this 0034 % code. If an error occurs for a different file, try regenerating the code 0035 % from the Import Tool. 0036 dataArray = textscan(fileID, formatSpec, endRow(1)-startRow(1)+1, 'Delimiter', delimiter, 'MultipleDelimsAsOne',1,'HeaderLines', startRow(1)-1, 'ReturnOnError', false); 0037 for block=2:length(startRow) 0038 frewind(fileID); 0039 dataArrayBlock = textscan(fileID, formatSpec, endRow(block)-startRow(block)+1, 'Delimiter', delimiter, 'MultipleDelimsAsOne',1,'HeaderLines', startRow(block)-1, 'ReturnOnError', false); 0040 for col=1:length(dataArray) 0041 dataArray{col} = [dataArray{col};dataArrayBlock{col}]; 0042 end 0043 end 0044 0045 %% Close the text file. 0046 fclose(fileID); 0047 0048 %% Post processing for unimportable data. 0049 % No unimportable data rules were applied during the import, so no post 0050 % processing code is included. To generate code which works for 0051 % unimportable data, select unimportable cells in a file and regenerate the 0052 % script. 0053 0054 %% Allocate imported array to column variable names 0055 VarName1 = dataArray{:, 1}; 0056 VarName2 = dataArray{:, 2}; 0057