this is my question,I write a txt named "data", when I load this data.txt,the programme is wrong..my programme and result show below,What should I do?

1 view (last 30 days)
code: load data.txt >>??? Error using ==> load Number of columns on line 2 of ASCII file C:\Users\Administrator\Desktop\data.txt must be the same as previous lines.
  3 Comments
Star Strider
Star Strider on 28 Aug 2015
The data are helpful, but we need to see the code you used to write the file.
Did you use the save function?
huang gangfeng
huang gangfeng on 28 Aug 2015
Edited: huang gangfeng on 28 Aug 2015
I write the code in the command windows.I didn't save function.the code is "load data.txt",then input ‘enter’,unfortunately,the result is wrong

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 28 Aug 2015
Edited: Stephen23 on 19 Oct 2021
The commas in your data file are the problem. Commas are used in many languages as the decimal radix (as opposed to the decimal point used in English), but are used in written English to separate digits groups in increments of 1000.
In either case these commas are not accepted by MATLAB's standard file parsing tools. You can either:
  • remove the commas from your file before reading it with a standard MATLAB function.
  • write your own file parsing function in MATLAB.
When I removed all of the commas from your file it loaded without error:
>> A = load('data.txt')
A =
313 2143 265 2277
216 965 224 949
196 1195 176 1356
190 1021 174 1263
171 958 162 905
149 1259 142 1517
107 696 109 680
93 1086 105 1116
79 627 69 588
72 1336 66 1566
70 1680 61 1657
56 1203 52 1085
52 1119 51 1044
51 1066 46 641
50 1249 42 338
48 695 39 1099
48 616 35 657
  2 Comments
huang gangfeng
huang gangfeng on 28 Aug 2015
thanks...but I want to know if there is a function that can remove all the commas once,because I have a similar data.txt document which have many numbers that have commas,if I remove these commas one by one by hand,It will waste much time
Stephen23
Stephen23 on 28 Aug 2015
Edited: Stephen23 on 28 Aug 2015
This is a basic feature of every text-editor. It is commonly called "Find and Replace". You can use it to locate text in a text file and replace it with some other text. It is very easy to use. Open the file in your favorite text-editor (it could be MATLAB!), press "ctrl" + "F", then search for ',' and replace all instances with ''. It will take you less time to do this than it takes you to read this comment.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 29 Aug 2015
textscan(strrep(fileread('new_data.txt') ',', ''), '%f%f%f%f')

Community Treasure Hunt

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

Start Hunting!