gettting Data from file

조회 수: 1 (최근 30일)
Max Müller
Max Müller 2014년 7월 24일
댓글: Michael Haderlein 2014년 7월 24일
hey Guys, I am using the fileread command to open a (.dat-file) which has the following disinge
-31050 0.500000 1 255 1.000000 5.000000
1 1 1 31.146388 16 217.650000 J_055
2 2 512 377.706052 128 157.723000 J_015
3 3 1 1.000000 1 136.334000 I_043
4 4 8 109.402337 64 185.494000 J_022
5 5 8 137.383648 64 189.172000 J_023
... ... ... ... ... ... ...
the 1st line is the header of the file. Question; How can i get Values of 1 column ? Or of a specific field ?
  댓글 수: 2
Max Müller
Max Müller 2014년 7월 24일
fid = fopen('name.dat');
ReadFile = textscan(fid,'%f','HeaderLines',1)
now how can i convert it into an array?
Michael Haderlein
Michael Haderlein 2014년 7월 24일
That should already result in an array. But you read all the file into one array. Most likely you rather want to use textscan(fid,'%d %*d %*d %*f %*d %*f %*s','headerlines',1);

댓글을 달려면 로그인하십시오.

채택된 답변

Michael Haderlein
Michael Haderlein 2014년 7월 24일
Do you want to get a whole line or a whole column?
Column:
>> fid=fopen('test.txt');
>> A=textscan(fid,'%d %d %d %f %d %f %s','headerlines',1);
>> A{3}
ans =
1
512
1
8
8
Line:
A=textscan(fid,'%f %f %f %f %f %f %s','headerlines',1);
>> B=cell2mat(A(1:end-1));
>> B(3,:)
ans =
3.0000 3.0000 1.0000 1.0000 1.0000 136.3340
  댓글 수: 2
Max Müller
Max Müller 2014년 7월 24일
Column.....Thanks a lot. It works....
Now can pls tell me how these (%f %f %f %f %f %f %s) things are named, so I can learn more about them....
Michael Haderlein
Michael Haderlein 2014년 7월 24일
The percentage sign indicates that some value will follow. f means, the value is a float, d means decimal, s means string and so on. Setting a * between the % and the type will skip this column. Details are in the help to the functions which are supporting this notation, e.g. http://www.mathworks.com/help/matlab/ref/textscan.html?searchHighlight=textscan#inputarg_formatSpec

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Michael Haderlein
Michael Haderlein 2014년 7월 24일
I think fileread is not the best option in this case. If you want to read the entire file, either use importdata or dlmread. If you want to skip some columns (e.g. for memory issues), you can use textscan (skip columns with the %*... specifier).
If you want to replace a specific value, either read the data, replace by A(32,4)=42; and save again or (a bit more tricky to realize) open the file, move to the position where you want to change something (fseek), write the new data and close the file. But then you need to know exactly the position in the file.
Best regards,
Michael
  댓글 수: 1
Max Müller
Max Müller 2014년 7월 24일
Sorry,
i am new to matlab. I just want this table to a [5,7] Array, where I can type.....A{3} and get the whole line and not (Index exceeds matrix dimensions.)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by