Read matrix from a txt file after a specific expression

Hello i m new with matlab, i need to read in a text file after a specific expression 'This section' and 3 header lines (A, B and C) a matrix with 3 colones and 10 rows
This section
A
B
C
1 23.0 5.33
2 12.4 6.07
10 22.4 64.07

3 件のコメント

Cedric
Cedric 2013 年 3 月 31 日
Is this line C constant? How does it look like? And can there be multiple 'This section' in the file?
Lila wagou
Lila wagou 2013 年 3 月 31 日
Tanks for the comment,The lines A, B and are not constants so i look to consider them like header lines, for the specific expression 'This section' there is one only.
Lila wagou
Lila wagou 2013 年 4 月 5 日
Please any suggestions ?

サインインしてコメントする。

 採用された回答

Cedric
Cedric 2013 年 4 月 5 日
編集済み: Cedric 2013 年 4 月 5 日

0 投票

You should use TEXTREAD with the 'heaerlines' parameter set to the number of lines (e.g. 3) that characterizes your header.
For example:
>> [id,temp,press] = textread('myData.txt', '%f %f %f', 'headerlines', 3)
id =
1
2
10
temp =
23.0000
12.4000
22.4000
press =
5.3300
6.0700
64.0700

3 件のコメント

Lila wagou
Lila wagou 2013 年 4 月 7 日
Thanks for you Mr Wannaz, but i need to read in a text file after a specific expression 'This section' which it is not a known line position in the txt file, so i must know first its position and i count 3 headerlines and i read the matrix.
Cedric
Cedric 2013 年 4 月 7 日
編集済み: Cedric 2013 年 4 月 7 日
Then you can tailor the following code to your needs:
fid = fopen('data.txt', 'r') ;
% Read lines until 'This section' found or FEOF.
while ~(strcmpi(fgetl(fid), 'this section') || feof(fid)) end
% Skip next 3 lines.
fgetl(fid) ; fgetl(fid) ; fgetl(fid) ;
% Read and reshape data.
data = reshape(fscanf(fid, '%f'), [], 3).' ;
fclose(fid) ;
Note that you should manage cases where 'This section', the three header lines, or data, are not found.
Lila wagou
Lila wagou 2013 年 4 月 7 日
It is perfect work, Thanks

サインインしてコメントする。

その他の回答 (0 件)

製品

Community Treasure Hunt

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

Start Hunting!

Translated by