I have to detect the Startrow and endrow from a file automatically from a .txt file...

3 views (last 30 days)
I am working on a program which would help me plot parameters such as force, displacement and time by extracting the data directly from the .txt file which has many junk lines which is of no use to me.... with my program i have to always manually enter the Startrow and endrow value.All i want to do now is to get those values automatically when i extract the data from the .txt file.
The data in the .txt file looks like this
<Mach-1 File>
<INFO>
Date: Tue, Jul 08, 2014
Time: 13:17:45.062
Mach-1 Motion Software Version: 4.3.0.1
Mach-1 System S/N: MA0561101
Mach-1 System Model: V500c
Load Cell Name: 10kg SN:1463878
Load Cell Type: Single-axis
Load Cell Calibration Factor: 1.000484
Load Cell Offset: -10.500000
Load Cell Calibration Date: 03/07/2014
<END INFO>
<Stress Relaxation>
Stage Axis: Position (z)
Load Cell Axis: Fz
Amplitude, mm: 3.0000
Velocity, mm/s: 1.0000
Number of Ramp: 2
Stop based on: Fixed Relaxation Time
Fixed Relaxation Time, s: 150
Relaxation Rate, gf/min: 0.0100
Time for Measurement of the Slope, s: 10
<DATA>
Time, s m Position (z), mm Fz, gf
0.000000 62.110000 -25.512351
0.010000 62.110000 -27.513320
.
.
.
.
307.280000 68.110000 67.032452
307.290000 68.110000 67.032452
<divider>
<END DATA>
All want are the numbers.

Answers (5)

Michael Haderlein
Michael Haderlein on 25 Jul 2014
Uh, what happened? Since your last edit, the file content is no more readable. Please reset it to code formatting.
Anyway, you can read it for instance this way:
fid=fopen('test.txt');
curline=fgetl(fid);
while ~strcmpi(curline,'<data>')
curline=fgetl(fid);
end
fgetl(fid);
data=textscan(fid,'%f %f %f');
fclose(fid)

Vinit
Vinit on 25 Jul 2014
Thank you for answering. This is a part from my code.
delimiter = '\t'; if nargin<=2 startRow = 26; endRow = 30742; end
Those are the parts in the program which i want to fill in the data automatically.
Since each file has different startrow and endrow
And I am very new to Matlab

Vinit
Vinit on 25 Jul 2014
With the program I am able to extract time,forceZ,Displacement as different cells so that i can get graphs by plotting one against the other.... but for each file the startrow and the endrow changes so it is problem to change it in the program each time.

Azzi Abdelmalek
Azzi Abdelmalek on 25 Jul 2014
fid = fopen('file.txt');
str={};
while ~feof(fid)
str{end+1,1}=fgetl(fid)
end
fclose(fid);
ii1=find(~cellfun(@isempty,regexpi(str,'<DATA>','match')))
ii2=find(~cellfun(@isempty,regexpi(str,'<divider>','match')))
data=cell2mat(cellfun(@str2num,str(ii1+2:ii2-1,:),'un',0))
  4 Comments
Vinit
Vinit on 25 Jul 2014
function [Time,Displacement,ForceZ] = importfile1(fileName) %IMPORTFILE1 Import numeric data from a text file as column vectors. % [DATE1,TUEJUL082014,VARNAME3] = IMPORTFILE1(FILENAME) Reads data from % text file FILENAME for the default selection. % % [DATE1,TUEJUL082014,VARNAME3] = IMPORTFILE1(FILENAME, STARTROW, ENDROW) % Reads data from rows STARTROW through ENDROW of text file FILENAME. % % Example: % [Date1,TueJul082014,VarName3] = importfile1('RELAXATION.txt',23, % 12107); % % See also TEXTSCAN.
% Auto-generated by MATLAB on 2014/07/17 12:07:48
%% Initialize variables. delimiter = '\t'; if nargin<=2 startRow = 26; endRow = 30742; end
%% Format string for each line of text: % column1: text (%s) % column2: text (%s) % column3: text (%s) % For more information, see the TEXTSCAN documentation. formatSpec = '%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, 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.
%% Allocate imported array to column variable names Time = dataArray{:, 1}; Displacement = dataArray{:, 2}; ForceZ = dataArray{:, 3}; TimeMat=str2double(Time(:,1)); ForceMat=str2double(ForceZ(:,1)); DispMat=str2double(Displacement(:,1));
This is the program ....and the file from which i have to extract the data...... #
What i want to do is 1. i want the program to start reading the data from the line where the readings start.
2. so my idea is is to write a code which checks every line for it's first letter ,as whether a alphabet or number.... if it is a alphabet it should not take the vale and the vise versa.
Michael Haderlein
Michael Haderlein on 28 Jul 2014
Sorry, but to me your file reading sequence doesn't make too much sense. If you simply replace everything from "Initialize variables" to "fclose(fileID);" by one of the two codes suggested, you should get what you want.

Sign in to comment.


Michael Haderlein
Michael Haderlein on 25 Jul 2014
Both codes suggested simply read in the numbers in the file. Replace the 'file.txt' resp. 'test.txt' by your file name and copy one of our codes into your command window or into the editor and run it.

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!