How to read a matrix from an ASCII file?

6 次查看(过去 30 天)
I have an ASCII file cuboid.mfd. It's content looks as follows:
Version : Marc Mentat 2010.1.0 (32bit)
=beg= 1 (magic)
1234
=end=
=beg= 2 (entities)
84
=end=
=beg= 3 (description)
=end=
=beg= 102 (nodes)
1 2.000000000000e+00 0.000000000000e+00 1.000000000000e+00
0 0
2 2.000000000000e+00 0.000000000000e+00 0.000000000000e+00
0 0
3 1.000000000000e+00 0.000000000000e+00 0.000000000000e+00
0 0
=end=
=beg= 205 (elements)
1 8 0 8
1 2 3 4
13 17 21 25
0 0 0 0
0 0 0 0
=end=
I would like to use the 6x4 matrix (nodes) and the 5x4 matrix (elements) for further calculations. The number of rows and columns of both of the matrices may vary but the structure of the file is always the same. I haven't found any appropriate command to import them. There should be an easy way how to do it.

回答(1 个)

Walter Roberson
Walter Roberson 2012-6-11
None of the built-in data importing commands can handle this, and there is no easy way to do it. You will need to use low-level I/O operations such as fopen(), fgetl(), fscanf(), fclose()
  3 个评论
Walter Roberson
Walter Roberson 2012-6-11
Nothing works? You are not able to fopen() the file and fgetl() a line at a time and display the line ?
Julius
Julius 2012-6-13
so far, nothing works.....sure I'm able to open the file and display the lines but working with text is something new to me so it took me 2 days to write this code:
fid = fopen('cuboid.mfd','r');
Headlines=textscan(fid,'%s',11,'delimiter','\n');
tline=fgetl(fid);
ix = 1;
while (~feof(fid))
Array{ix}=tline;
tline=fgetl(fid);
ix=ix + 1;
end
fclose(fid);
Now I have only those two matrices separated by '=end=' and don't know how to continue. I tried to convert the lines into numbers to be able to work with ..but it didn't work either

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Low-Level File I/O 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by