gettting Data from file
조회 수: 1 (최근 30일)
이전 댓글 표시
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
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
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
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
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
참고 항목
카테고리
Help Center 및 File Exchange에서 String Parsing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!