I have to detect the Startrow and endrow from a file automatically from a .txt file...
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(5 个)
Michael Haderlein
2014-7-25
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 个评论
Vinit
2014-7-25
1 个评论
Michael Haderlein
2014-7-25
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
2014-7-25
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 个评论
Michael Haderlein
2014-7-28
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
2014-7-25
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Large Files and Big Data 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!