Importing data from text file into Matlab

In summary, the <code>importdata</code> function in Matlab can import data from various file formats, including .txt, .csv, .dat, .xls, and .mat. It also allows you to specify the delimiter used in the text file and skip header lines when importing the data. Once imported, the data can be easily viewed and manipulated in Matlab.
  • #1
quetzal
6
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

  • datafile.txt
    17.1 KB · Views: 545
Last edited:
Physics news on Phys.org
  • #2
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, :);
 
  • #3
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.
 
  • #4
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:
  • #5
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!
 

Related to Importing data from text file into Matlab

1. How do I import a text file into Matlab?

To import a text file into Matlab, you can use the importdata function. This function automatically detects the format of the data in the text file and imports it into a variable in the workspace.

2. What file formats can be imported into Matlab using the importdata function?

The importdata function can import data from text files with various formats, such as .txt, .csv, .dat, and .xls. It can also import data from .mat files and Excel spreadsheets.

3. Can I specify the delimiter when importing a text file into Matlab?

Yes, you can specify the delimiter used in the text file by using the importdata function with the 'delimiter' parameter. For example, if your text file uses commas as delimiters, you can use importdata('data.txt','delimiter',',') to import the data.

4. How can I view the imported data in Matlab?

Once the data is imported into a variable, you can view it by simply typing the variable name in the command window. If your data is in a table format, you can use the disp function to display it in a table view.

5. Is it possible to import only a specific portion of a text file into Matlab?

Yes, you can use the importdata function with the 'headerlines' parameter to specify the number of header lines to skip when importing the data. This allows you to import only the data you need from a text file.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
16
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
14
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
32
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
Back
Top