How to convert a text file into an array

3 次查看(过去 30 天)
I have a text file that looks something like this:
.2668067E-02 .2663488E-02 .2662231E-02 .2662557E-02 .2667599E-02
.2677025E-02 .2674504E-02 .2649519E-02 .2634860E-02 .2667714E-02
.2739410E-02 .2760168E-02 .2696653E-02 .2616312E-02 .2589599E-02
.2655084E-02 .2721280E-02 .2773444E-02 .2776801E-02 .2739715E-02
.2719981E-02 .2739942E-02 .2674718E-02 .2535300E-02 .2691837E-02
.3064602E-02 .3193767E-02 .2785211E-02 .2263564E-02 .2276346E-02
.2812114E-02 .3083767E-02 .2638711E-02 .2749027E-02 .3258084E-02
.3323313E-02 .2719941E-02 .2265815E-02 .2885909E-02 .3676279E-02
.3445298E-02 .2656125E-02 .2620043E-02 .3395507E-02 .3674076E-02
only with many thousand more data points. The data reads from left to right, then steps down to the next row. I am trying to read this text file into a single array (i.e. - I need a 5000 X 1 matrix, not a 1000 X 5 martix)
It would be unreasonable for me to try and manipulate the text file into a single column. Any help would be greatly appreciated. Thanks!
Edit: The following is an example of what I am trying to accomplish
Here is a text file that I am reading
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
and I'm trying to convert it into an array that looks like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

回答(1 个)

Star Strider
Star Strider 2015-10-22
编辑:Star Strider 2015-10-22
Use the textscan function to read the file. The reshape function should do what you want to convert it to a (5000x1) vector.
  2 个评论
Jared
Jared 2015-10-23
Thanks for your quick response! I'm not familiar with the reshape function and how it actually reshapes the data. Please refer to my example that I added above. The position of each data point within the array is important. I need to read the data points in from left to right then step down to the next row. Would the reshape function be able to put the data in the correct order? Thanks!
Star Strider
Star Strider 2015-10-23
My pleasure!
The reshape function reshapes a vector or matrix into the form you want it. The number of elements in the argument and result arrays must be equal, but that is the only restriction.
Here, it works just as you want it to:
A = [1:5; 6:10; 11:15]
Ar = reshape(A', [], 1)
A =
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
Ar =
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Notice that you have to transpose the matrix in the reshape call.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by