Importing data from text file into Matlab

AI Thread Summary
Assistance is needed to extract two specific matrices from a text file into MATLAB: a 60x3 matrix of node coordinates and a 24x8 matrix of node numbers for elements. The second matrix includes unwanted rows that likely contain connectivity arrays, which should be excluded. A suggested solution is to manually split the data into two separate text files, one for each matrix, to simplify the import process. The first row of the second matrix should conform to a specified format for proper data structure. This approach is recommended for those who only need to perform this task once.
quetzal
Messages
5
Reaction score
0

Homework Statement



I would need some help with extracting two matrices from a text file into Matlab.
First is the 60x3 matrix of node coordinates.

Second is the 24x8 matrix of node numbers assigned to elements.
I have 24 brick elements so each of the elements has 8 nodes.
But there are some rows of numbers which are not the node numbers but probably some connectivity arrays and I don't want them there.
According to the attached file, the first row of the second matrix should look like this:
elements(1,:)=[ 1 2 3 4 13 17 24 25 ];
I don't know how to write that code :(
I 'd really appreciate someone's help.
 

Attachments

Last edited:
Physics news on Phys.org
What is the context? If you just need to do this once, by far the simplest approach is to manually break the file up into two text files with only the data in it. So make

matrixA.txt with

Code:
        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
                   4  1.000000000000e+00  0.000000000000e+00  1.000000000000e+00
                   0                   0
                   5  2.000000000000e+00  0.000000000000e+00  2.000000000000e+00
                   0                   0
                   6  1.000000000000e+00  0.000000000000e+00  2.000000000000e+00
                   0                   0
                   7  2.000000000000e+00  0.000000000000e+00  3.000000000000e+00
                   0                   0
                   8  1.000000000000e+00  0.000000000000e+00  3.000000000000e+00
                   0                   0
                   9  0.000000000000e+00  0.000000000000e+00  0.000000000000e+00
                   0                   0
                  10  0.000000000000e+00  0.000000000000e+00  1.000000000000e+00
                   0                   0
                  11  0.000000000000e+00  0.000000000000e+00  2.000000000000e+00
                   0                   0
                  12  0.000000000000e+00  0.000000000000e+00  3.000000000000e+00
                   0                   0
                  13  2.000000000000e+00  1.000000000000e+00  1.000000000000e+00
                   0                   0
                  14  2.000000000000e+00  2.000000000000e+00  1.000000000000e+00
                   0                   0
                  15  2.000000000000e+00  3.000000000000e+00  1.000000000000e+00
                   0                   0
                  16  2.000000000000e+00  4.000000000000e+00  1.000000000000e+00
                   0                   0
                  17  2.000000000000e+00  1.000000000000e+00  0.000000000000e+00
                   0                   0
                  18  2.000000000000e+00  2.000000000000e+00  0.000000000000e+00
                   0                   0
                  19  2.000000000000e+00  3.000000000000e+00  0.000000000000e+00
                   0                   0
                  20  2.000000000000e+00  4.000000000000e+00  0.000000000000e+00
                   0                   0
                  21  1.000000000000e+00  1.000000000000e+00  0.000000000000e+00
                   0                   0
                  22  1.000000000000e+00  2.000000000000e+00  0.000000000000e+00
                   0                   0
                  23  1.000000000000e+00  3.000000000000e+00  0.000000000000e+00
                   0                   0
                  24  1.000000000000e+00  4.000000000000e+00  0.000000000000e+00
                   0                   0
                  25  1.000000000000e+00  1.000000000000e+00  1.000000000000e+00
                   0                   0
                  26  1.000000000000e+00  2.000000000000e+00  1.000000000000e+00
                   0                   0
                  27  1.000000000000e+00  3.000000000000e+00  1.000000000000e+00
                   0                   0
                  28  1.000000000000e+00  4.000000000000e+00  1.000000000000e+00
                   0                   0
                  29  2.000000000000e+00  1.000000000000e+00  2.000000000000e+00
                   0                   0
                  30  2.000000000000e+00  2.000000000000e+00  2.000000000000e+00
                   0                   0
                  31  2.000000000000e+00  3.000000000000e+00  2.000000000000e+00
                   0                   0
                  32  2.000000000000e+00  4.000000000000e+00  2.000000000000e+00
                   0                   0
                  33  1.000000000000e+00  1.000000000000e+00  2.000000000000e+00
                   0                   0
                  34  1.000000000000e+00  2.000000000000e+00  2.000000000000e+00
                   0                   0
                  35  1.000000000000e+00  3.000000000000e+00  2.000000000000e+00
                   0                   0
                  36  1.000000000000e+00  4.000000000000e+00  2.000000000000e+00
                   0                   0
                  37  2.000000000000e+00  1.000000000000e+00  3.000000000000e+00
                   0                   0
                  38  2.000000000000e+00  2.000000000000e+00  3.000000000000e+00
                   0                   0
                  39  2.000000000000e+00  3.000000000000e+00  3.000000000000e+00
                   0                   0
                  40  2.000000000000e+00  4.000000000000e+00  3.000000000000e+00
                   0                   0
                  41  1.000000000000e+00  1.000000000000e+00  3.000000000000e+00
                   0                   0
                  42  1.000000000000e+00  2.000000000000e+00  3.000000000000e+00
                   0                   0
                  43  1.000000000000e+00  3.000000000000e+00  3.000000000000e+00
                   0                   0
                  44  1.000000000000e+00  4.000000000000e+00  3.000000000000e+00
                   0                   0
                  45  0.000000000000e+00  1.000000000000e+00  0.000000000000e+00
                   0                   0
                  46  0.000000000000e+00  2.000000000000e+00  0.000000000000e+00
                   0                   0
                  47  0.000000000000e+00  3.000000000000e+00  0.000000000000e+00
                   0                   0
                  48  0.000000000000e+00  4.000000000000e+00  0.000000000000e+00
                   0                   0
                  49  0.000000000000e+00  1.000000000000e+00  1.000000000000e+00
                   0                   0
                  50  0.000000000000e+00  2.000000000000e+00  1.000000000000e+00
                   0                   0
                  51  0.000000000000e+00  3.000000000000e+00  1.000000000000e+00
                   0                   0
                  52  0.000000000000e+00  4.000000000000e+00  1.000000000000e+00
                   0                   0
                  53  0.000000000000e+00  1.000000000000e+00  2.000000000000e+00
                   0                   0
                  54  0.000000000000e+00  2.000000000000e+00  2.000000000000e+00
                   0                   0
                  55  0.000000000000e+00  3.000000000000e+00  2.000000000000e+00
                   0                   0
                  56  0.000000000000e+00  4.000000000000e+00  2.000000000000e+00
                   0                   0
                  57  0.000000000000e+00  1.000000000000e+00  3.000000000000e+00
                   0                   0
                  58  0.000000000000e+00  2.000000000000e+00  3.000000000000e+00
                   0                   0
                  59  0.000000000000e+00  3.000000000000e+00  3.000000000000e+00
                   0                   0
                  60  0.000000000000e+00  4.000000000000e+00  3.000000000000e+00
                   0                   0

and matrixB.txt with
Code:
 1                   8                   0                   8
                   1                   2                   3                   4
                  13                  17                  21                  25
                   0                   0                   0                   0
                   0                   0                   0                   0
                   2                   8                   0                   8
                  13                  17                  21                  25
                  14                  18                  22                  26
                   0                   0                   0                   0
                   0                   0                   0                   0
                   3                   8                   0                   8
                  14                  18                  22                  26
                  15                  19                  23                  27
                   0                   0                   0                   0
                   0                   0                   0                   0
                   4                   8                   0                   8
                  15                  19                  23                  27
                  16                  20                  24                  28
                   0                   0                   0                   0
                   0                   0                   0                   0
                   5                   8                   0                   8
                   5                   1                   4                   6
                  29                  13                  25                  33
                   0                   0                   0                   0
                   0                   0                   0                   0
                   6                   8                   0                   8
                  29                  13                  25                  33
                  30                  14                  26                  34
                   0                   0                   0                   0
                   0                   0                   0                   0
                   7                   8                   0                   8
                  30                  14                  26                  34
                  31                  15                  27                  35
                   0                   0                   0                   0
                   0                   0                   0                   0
                   8                   8                   0                   8
                  31                  15                  27                  35
                  32                  16                  28                  36
                   0                   0                   0                   0
                   0                   0                   0                   0
                   9                   8                   0                   8
                   7                   5                   6                   8
                  37                  29                  33                  41
                   0                   0                   0                   0
                   0                   0                   0                   0
                  10                   8                   0                   8
                  37                  29                  33                  41
                  38                  30                  34                  42
                   0                   0                   0                   0
                   0                   0                   0                   0
                  11                   8                   0                   8
                  38                  30                  34                  42
                  39                  31                  35                  43
                   0                   0                   0                   0
                   0                   0                   0                   0
                  12                   8                   0                   8
                  39                  31                  35                  43
                  40                  32                  36                  44
                   0                   0                   0                   0
                   0                   0                   0                   0
                  13                   8                   0                   8
                   4                   3                   9                  10
                  25                  21                  45                  49
                   0                   0                   0                   0
                   0                   0                   0                   0
                  14                   8                   0                   8
                  25                  21                  45                  49
                  26                  22                  46                  50
                   0                   0                   0                   0
                   0                   0                   0                   0
                  15                   8                   0                   8
                  26                  22                  46                  50
                  27                  23                  47                  51
                   0                   0                   0                   0
                   0                   0                   0                   0
                  16                   8                   0                   8
                  27                  23                  47                  51
                  28                  24                  48                  52
                   0                   0                   0                   0
                   0                   0                   0                   0
                  17                   8                   0                   8
                   6                   4                  10                  11
                  33                  25                  49                  53
                   0                   0                   0                   0
                   0                   0                   0                   0
                  18                   8                   0                   8
                  33                  25                  49                  53
                  34                  26                  50                  54
                   0                   0                   0                   0
                   0                   0                   0                   0
                  19                   8                   0                   8
                  34                  26                  50                  54
                  35                  27                  51                  55
                   0                   0                   0                   0
                   0                   0                   0                   0
                  20                   8                   0                   8
                  35                  27                  51                  55
                  36                  28                  52                  56
                   0                   0                   0                   0
                   0                   0                   0                   0
                  21                   8                   0                   8
                   8                   6                  11                  12
                  41                  33                  53                  57
                   0                   0                   0                   0
                   0                   0                   0                   0
                  22                   8                   0                   8
                  41                  33                  53                  57
                  42                  34                  54                  58
                   0                   0                   0                   0
                   0                   0                   0                   0
                  23                   8                   0                   8
                  42                  34                  54                  58
                  43                  35                  55                  59
                   0                   0                   0                   0
                   0                   0                   0                   0
                  24                   8                   0                   8
                  43                  35                  55                  59
                  44                  36                  56                  60
                   0                   0                   0                   0
                   0                   0                   0                   0


then you can read each file using the command

matrix = textread('matrixA.txt');

You can then rework the read in data. For example, the first matrix seems to have a line of two zeros between each meaningful entry, and textread() fills in the two omitted spots with zeros, resulting in a row of four zeros every other entry that needs removal. You can easily do this like so:

A = matrix(1:2:end, :);
 
Thanks RoshanBBQ!
I know that the easiest way is extracting the two matrices manually. But I'm just curious if there exists a command/code to locate them within the text file. I will need to do it many times repeatedly.
 
quetzal said:
Thanks RoshanBBQ!
I know that the easiest way is extracting the two matrices manually. But I'm just curious if there exists a command/code to locate them within the text file. I will need to do it many times repeatedly.

http://www.mathworks.com/help/techdoc/ref/f16-5702.html#f16-14516

MATLAB has a set of low-level file reading commands, which can do practically anything given some semblance of structure in the data and enough time and code length.
 
Last edited by a moderator:
I know about the low-level O/I functions but I really don't know how to handle them. It is something new to me because it is a bit different from the common mathematical programing i was used to.
I've done this so far:
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);

That can reduce the text file to only those lines which belong to the mentioned matrices separated by a line containing =end=.
I don't know how to continue...I also tried to convert the arrays into numbers so as to be able to handle them as I'm used to. For example:

Code:
>> char(Array(101))

ans =

                  51  0.000000000000e+00  3.000000000000e+00  1.000000000000e+00
but:
Code:
>> char(Array(101))*2

ans =

  Columns 1 through 13

    64    64    64    64    64    64    64    64    64    64    64    64    64

  Columns 14 through 26

    64    64    64    64    64   106    98    64    64    96    92    96    96

  Columns 27 through 39

    96    96    96    96    96    96    96    96    96    96   202    86    96

  Columns 40 through 52

    96    64    64   102    92    96    96    96    96    96    96    96    96

  Columns 53 through 65

    96    96    96    96   202    86    96    96    64    64    98    92    96

  Columns 66 through 78

    96    96    96    96    96    96    96    96    96    96    96   202    86

  Columns 79 through 80

    96    96
and that is nonsense!
 
Back
Top