Error using ==> load Number of columns on line 1 of ASCII file must be the same as previous lines.

32 views (last 30 days)
My text file is in the current directory.
This is my m-file.
clear all
close all
fontsize = 12;
myfile = 'homework0file.txt';
mydata = load(myfile);
x = mydata(:,1);
y = mydata(:,2);
This is my txt file
% column 1 is time in seconds
% column 2 is distance in cm
1 3.35959
2 -9.14439
3 -2.67077
4 -11.58844
5 -11.13751
6 -5.16964
7 -6.10970
8 -17.84179
9 -20.72885
10 -17.68507
11 -19.91041
12 -9.58301
13 -12.87876
14 -12.57608
15 -19.52674
16 -4.54303
17 -6.92412
18 -10.62736
19 -2.85174
20 -16.11786
21 -7.09710
22 -7.56091
23 -7.55522
24 -15.10473
25 -7.71212
26 -13.60417
27 -16.27512
28 -18.42288
29 -2.73529
30 -15.92161
31 -11.21040
32 -18.46056
33 -12.07989
34 -32.62426
35 -17.60871
36 -19.98505
37 -17.10116
38 -11.49287
39 -12.27743
40 3.61078
41 -14.00241
42 -11.75023
43 -20.98521
44 -13.34168
45 -19.36584
46 -9.78791
47 -4.02839
48 -7.17023
49 -14.90434
50 -21.41380
51 0.22009
52 4.35030
53 -9.15650
54 -16.92931
55 -1.61560
56 -14.14859
57 -13.28867
58 -3.79536
59 -19.69654
60 -23.69728
61 -7.05521
62 -7.19483
63 -9.33400
64 -6.52321
65 -2.42432
66 -3.20687
67 -13.97999
68 -4.33020
69 -8.78727
70 -13.53880
71 -18.35314
72 -5.47120
73 -12.47536
74 -9.67496
75 -15.55063
76 -20.85360
77 -8.79890
78 -10.43497
79 -1.60680
80 -4.38807
when the m-file is run
the error in the title shows up
WHY DO I WASTE SO MUCH TIME ON SUCH A POINTLESS ERROR
  1 Comment
Oleg Komarov
Oleg Komarov on 28 Aug 2011
Please format the code and the data: http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup#answer_18099

Sign in to comment.

Answers (2)

Fangjun Jiang
Fangjun Jiang on 29 Aug 2011
What version of MATLAB are you using? I tried on R2007b. It is fine.
>> mydata=load('test.txt')
mydata =
1.0000 3.3596
2.0000 -9.1444
3.0000 -2.6708
4.0000 -11.5884
Or try importdata()
>> a=importdata('test.txt')
a =
1.0000 3.3596
2.0000 -9.1444
3.0000 -2.6708
4.0000 -11.5884
Or
>> [x,y]=textread('test.txt','%f %f','headerlines',2);
>> [x(1:5) y(1:5)]
ans =
1.0000 3.3596
2.0000 -9.1444
3.0000 -2.6708
4.0000 -11.5884
5.0000 -11.1375

Walter Roberson
Walter Roberson on 29 Aug 2011
load() is not suitable for files that contain text (even in the form of matlab comments.)
You should use textscan() or dlmread(), specifying to them that you want to skip two header lines or that you want to treat '%' as indicating a comment.
  10 Comments
Walter Roberson
Walter Roberson on 7 Sep 2019
I was probably wrong in my initial answer: % comments at the beginning of lines are accepted by load of text files.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!