How to read a matrix from an ASCII file?

7 views (last 30 days)
Julius
Julius on 11 Jun 2012
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.

Answers (1)

Walter Roberson
Walter Roberson on 11 Jun 2012
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 Comments
Walter Roberson
Walter Roberson on 11 Jun 2012
Nothing works? You are not able to fopen() the file and fgetl() a line at a time and display the line ?
Julius
Julius on 13 Jun 2012
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

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!