I have to detect the Startrow and endrow from a file automatically from a .txt file...
    7 views (last 30 days)
  
       Show older comments
    
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.
0 Comments
Answers (5)
  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)
0 Comments
  Vinit
 on 25 Jul 2014
        1 Comment
  Michael Haderlein
      
 on 25 Jul 2014
				Have you tried to run my piece of code? It will go to the data tag and then reads until the end of the table.
  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
  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.
  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.
0 Comments
See Also
Categories
				Find more on Text Files 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!

