TheExplosiveSheep
TheExplosiveSheep

Reputation: 64

Reading column vector from .txt file in matlab

I am trying to read the following sequence of numbers from a textfile:

1323211
11
1163211
11
1512321112
14096321
2
1256321221024642
1
1
1256321221024642
10296844
4102782
3609910
0
492872
492840
61116
0
01201
01201
00149
00000
1989016
1982261
436984
6755
6739
6734
0
00034
00034
00034
00000

In order to do this I am using the method outlined here: http://www.mathworks.se/help/matlab/ref/fscanf.html#bt_j35z-2_1

I can verify the contents of the file with the type command, but the reading goes wrong for some reason, giving this output:

>> fid = fopen('file.txt', 'r')

fid =

     5

>> A = fscanf(fid, '%f')

A =

   1.0e+15 *

    0.0000
    0.0000
    0.0000
    0.0000
    0.0000
    0.0000
    0.0000
    1.2563
    0.0000
    0.0000
    1.2563
    0.0000
    0.0000
    0.0000
         0
    0.0000
    0.0000
    0.0000
         0
    0.0000
    0.0000
    0.0000
         0
    0.0000
    0.0000
    0.0000
    0.0000
    0.0000
    0.0000
         0
    0.0000
    0.0000
    0.0000
         0

I use %f in the example, but %d returns an empty vector. csvread, dlmread and textscan all yield the same result. What causes this?

Upvotes: 0

Views: 141

Answers (1)

osmanpontes
osmanpontes

Reputation: 546

The output is correct, but the format doesn't tell you what you want to see. Now you're probably using format short. You should just use the command below to see the whole number:

format long

Another thing you can do is click on "A" in the Workspace window to see the matrix in a table. Remember to check the tab View and choose the "Number Display Format" you want to use.

Upvotes: 1

Related Questions