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 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!