gettting Data from file

3 views (last 30 days)
Max Müller
Max Müller on 24 Jul 2014
Commented: Michael Haderlein on 24 Jul 2014
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 Comments
Max Müller
Max Müller on 24 Jul 2014
fid = fopen('name.dat');
ReadFile = textscan(fid,'%f','HeaderLines',1)
now how can i convert it into an array?
Michael Haderlein
Michael Haderlein on 24 Jul 2014
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);

Sign in to comment.

Accepted Answer

Michael Haderlein
Michael Haderlein on 24 Jul 2014
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 Comments
Max Müller
Max Müller on 24 Jul 2014
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 on 24 Jul 2014
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

Sign in to comment.

More Answers (1)

Michael Haderlein
Michael Haderlein on 24 Jul 2014
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 Comment
Max Müller
Max Müller on 24 Jul 2014
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.)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!