MATLAB: How to read a 1000 digit number from a text file into a matrix

read into matrixtext file

I have a 1000 digit number in a text file that is formatted like a 20 x 50 matrix without any delimiters between the numbers.
73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450
I need to be able to work with the digits in this number individually, so I want to read it into a matrix or an array to work with in matlab. I have tried using dlmread() and fscanf(), but I can't see how to read the digits into individual cells of a matrix without delimiters.
If it's any help, I am trying to solve this problem from Project Euler.

Best Answer

Problem 8? Did that one - the answer is... :) I'm happy to see someone using MATLAB to solve these problems. It is a great way to learn MATLAB, as well as to learn a few nice tools in computing and in mathematics.
Ok, the trick is, you are thinking about these as NUMBERS. Don't do that. Think of them as text, as characters, at least in order to read them in. Read it in as a text file, with 1000 characters. So create a vector, of length 1000, full of characters.
Then convert each character into a number. For example, you can do that easily enough.
str = '23543563634654747568567457585674858568757856873698765969676589560875685'
str- '0'
ans =
Columns 1 through 24
2 3 5 4 3 5 6 3 6 3 4 6 5 4 7 4 7 5 6 8 5 6 7 4
Columns 25 through 48
5 7 5 8 5 6 7 4 8 5 8 5 6 8 7 5 7 8 5 6 8 7 3 6
Columns 49 through 71
9 8 7 6 5 9 6 9 6 7 6 5 8 9 5 6 0 8 7 5 6 8 5
This is a nice trick to convert ascii characters into a numeric representation.
Do you ABSOLUTELY need to do it that way? OF course not. textread can do it for you, without needing to do the above conversion. TRY IT. For example, try this:
S = textread('untitled2','%1u')';
There are always multiple ways to solve a problem. Find one, and don't worry about if there is abetter way, as long as your solution works. This is true for PE problems too, although you need to be careful there. Brute force solutions will often be unmanageable there, so you will need to find a viable solution.