I want to import 'txt' data

2 views (last 30 days)
pty0220
pty0220 on 25 Jan 2016
Answered: Walter Roberson on 25 Jan 2016
I want to import 'txt' data
and I found some open source in Internet
that source look like this
1.
%[f,p]=uigetfile('*.txt','Choose the DST txt file');
%filename = strcat(p,f)
filename = f
delimiter = {'\t',' '};
formatSpec = '%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%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, 'Delimiter', delimiter, 'ReturnOnError', false);
%%Close the text file.
fclose(fileID);
%%Create output variable
Total = [dataArray{1:end-1}];
%%Clear temporary variables
clearvars filename delimiter formatSpec fileID dataArray ans;
%%Arrange Total and makeTotal_1=C
%Total_table
for i=1:size(Total,1)
if strcmp(Total{i,1},'All')==1;
StartingPoint=i;
end
if strcmp(Total{i,1},'Accuray')==1;
EndPoint=i-1;
end
end
Total_1=[];
a=Total{StartingPoint,1};
b=Total{StartingPoint,2};
c=Total{StartingPoint,3};
name = strcat(a,b,c);
Total_1{1,1}=name;
for i=1:7
Total_1{1,i+1}=Total{ StartingPoint,i+3};
end
for i=StartingPoint+1:EndPoint
for j=1:8
Total_1{i-13,j}=Total{i,j};
end
end
assignin('base','Total', Total);
assignin('base','Total_1', Total_1);
2.
unction [DateTime,TemperatureF] = importfile(filename, startRow, endRow)
%IMPORTFILE1 Import numeric data from a text file as column vectors.
% [DATETIME,TEMPERATUREF] = IMPORTFILE1(FILENAME) Reads data from text
% file FILENAME for the default selection.
%
% [DATETIME,TEMPERATUREF] = IMPORTFILE1(FILENAME, STARTROW, ENDROW) Reads
% data from rows STARTROW through ENDROW of text file FILENAME.
%
% Example:
% [DateTime,TemperatureF] = importfile1('weatherNatick.txt',1, 168);
%
% See also TEXTSCAN.
% Auto-generated by MATLAB on 2014/06/06 14:42:59
%%Initialize variables.
delimiter = ',';
if nargin<=2
startRow = 1;
endRow = inf;
end
%%Read columns of data as strings:
% For more information, see the TEXTSCAN documentation.
formatSpec = '%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, 'HeaderLines', startRow(1)-1, 'ReturnOnError', false);
for block=2:length(startRow)
frewind(fileID);
dataArrayBlock = textscan(fileID, formatSpec, endRow(block)-startRow(block)+1, 'Delimiter', delimiter, '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);
and than I realized that code is not written by person and it was written automatically such as GUI
So I want to find this Auto-generated code ,
Where can I find it !?!?

Accepted Answer

Walter Roberson
Walter Roberson on 25 Jan 2016
After you select your source and make the adjustments, click on "Generate MATLAB Code" to generate the importfile() routine.

More Answers (0)

Categories

Find more on Large Files and Big Data in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!