Reading values from a text file and converting to array.
조회 수: 2 (최근 30일)
이전 댓글 표시
I have a text file 'U.txt' which has data as follows:
# x 0 0.3 0.4
# y 0 0 0
# z 0 0 0
# Time
0.000125 (0.101993 0 0) (0.100009 0 0) (0.100009 0 0)
0.00025 (0.14199 0 0) (0.0998676 0 0) (0.0976896 0 0)
0.000375 (0.161106 0 0) (0.0989464 0 0) (0.0895835 0 0)
0.0005 (0.178717 0 0) (0.0960872 0 0) (0.0763535 0 0)
I want to extract the values of each column (without the header) into respective arrays.example:
Array1= 0.000125 0.00025 0.000375 0.0005
Array2= 0.101993 0.14199 0.161106 0.178717
Array3= 0.100009 0.0998676 0.0989464 0.096872
Array4= 0.100009 0.0976896 0.0895835 0.0763535
Kindly help me out. Thanks in advance
채택된 답변
Cedric
2014년 5월 28일
편집: Cedric
2014년 5월 28일
Here is one way to achieve what you want to do:
>> content = fileread( 'myFile.txt' ) ;
>> data = textscan( content, '%f (%f %*d%*d) (%f %*d%*d) (%f%*[^\n]', ...
'HeaderLines', 4 ) ;
where columns are stored into cells of cell array data:
>> data
data =
[4x1 double] [4x1 double] [4x1 double] [4x1 double]
>> data{1}
ans =
1.0e-03 *
0.1250
0.2500
0.3750
0.5000
>> data{2}
ans =
0.1020
0.1420
0.1611
0.1787
>> data{3}
ans =
0.1000
0.0999
0.0989
0.0961
>> data{4}
ans =
0.1000
0.0977
0.0896
0.0764
댓글 수: 3
Cedric
2014년 5월 28일
Yes, the default display format is short, which rounds variables content when displayed in the command window. If you really want to see variables' content in higher precision, you can execute
>> format long
추가 답변 (1개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Text Files에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!