textscan (read data from second and fourth line)

i am having some trouble using textscan
i have data in input file as
AAA BBB CCC DDD
10 20 30 40
EEE FFF
40 50
how to read this data
i can read data if it is only in the first line of input file but having trouble it data is in multiple line

2 件のコメント

Walter Roberson
Walter Roberson 2012 年 5 月 14 日
Do you know the exact number of values on the 2nd and 4th line?
Haris Hameed
Haris Hameed 2012 年 5 月 14 日
yes 4 values in second line and 2 in fourth line
line one and line three contains the parameter name

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

 採用された回答

Walter Roberson
Walter Roberson 2012 年 5 月 14 日

0 投票

L2cell = textscan(fid, '%f%f%f%f', 1, 'HeaderLines', 1, 'CollectOutput', 1);
L4cell = textscan(fid, '%f%f', 1, 'HeaderLines', 1, 'CollectOutput', 1);

8 件のコメント

Haris Hameed
Haris Hameed 2012 年 5 月 14 日
fid1=fopen('input3.dat','r');
L2cell = textscan(fid1, '%f %f %f %f', 1, 'HeaderLines', 1, 'CollectOutput', 1)
L4cell = textscan(fid1, '%f %f', 1, 'HeaderLines', 1, 'CollectOutput', 1)
fclose(fid1);
AAA=L2cell{1,1}
BBB=L4cell{1,1}
i get
AAA =
10 20 30 40
BBB =
Empty matrix: 0-by-2
still not getting values for fourth row
Haris Hameed
Haris Hameed 2012 年 5 月 14 日
also i want to use these values separately
like i try DDD=L2cell{1,2}
i get ??? Index exceeds matrix dimensions.
Error in ==> check at 12
DDD=L2cell{1,2}
Thomas
Thomas 2012 年 5 月 14 日
try
L4cell = textscan(fid, '%f%f', 1, 'HeaderLines', 3, 'CollectOutput', 1);
L4cell{:}
Thomas
Thomas 2012 年 5 月 14 日
to get it as separate values use:
p=L2cell{:}
m=L4cell{:}
>> m(1)
ans=
40
Walter Roberson
Walter Roberson 2012 年 5 月 14 日
If you want the values in separate cells, leave off the "CollectOutput" option. It would probably be easier to use AAA(1), AAA(2) and so on.
HeaderLines 3 would only apply if you had reset to the beginning of the file.
Walter Roberson
Walter Roberson 2012 年 5 月 14 日
Okay, looks like you need to add
fgetl(fid);
between the two textscan() calls, as the cursor is left at the end of the line rather than at the beginning of the next line.
Thomas
Thomas 2012 年 5 月 14 日
Walter, you do not need to reset to the beginning of the file at-least not in 2012a. The following works just fine.. Though it might not be the optimal way of doing this.. :)
fid=fopen('input1.dat','r');
L2cell = textscan(fid, '%f%f%f%f', 1, 'HeaderLines', 1);
L4cell = textscan(fid, '%f%f', 1, 'HeaderLines', 3);
L2cell{:}
L4cell{:}
Daimien Burks
Daimien Burks 2012 年 5 月 14 日
Don't mean to steal the thread, but would this work on columns as well? Like get AAA/10 and CCC/30?

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

その他の回答 (0 件)

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by