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 Center および File ExchangeString Parsing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by