


I only do this so that if I repeat these steps in a new MATLAB session, I'll be sure to get a different result. Before I do that, though, I'll shuffle the random number generator. ''Īgain, this is the position shown at top of this post. To make the for-loop work, I'm going to add an "extra" value to the first_lines vector that points to a nonexistent line just past the of the file. Next, I'll make a cell array such that that each cell contains all the lines for one position. So there are positions starting on lines 1, 12, and 23.

Each of these lines is the start of an entry for one chess position. idx is a cell array, so I'll use cellfun and find to identify all the lines that contain the matching string. This shows us that this string is found twice in the first 15 lines of the file. But how many positions are there? I'm going to find the starting line of each position by searching for the string "[Event " at the beginning a line. Let's split the data into lines using strsplit. You can see that there are about 106,000 characters in the file. The function fileread is just the ticket. Here's what I came up with.įirst, read in the entire file. There are a lot of different ways to do this. Write out the rearranged chunks to a new text file.Split the data into chunks, one chunk for each position.(Fun thing to do on a Saturday morning, right?) None of the chess software programs that I have can do this, so I decided to tackle it with MATLAB. I wanted to shuffle the positions in this file randomly. Can you figure out how White wins by capturing the Bishop on c8 with his rook? (If you're interested in how the position is encoded in the text, see the Wikipedia article on Forsyth-Edwards Notation, or FEN.) The first is the position I showed above, with White to move. Here are lines 2,759 through 2,784 from the file. I have a text file, tactics.pgn, that is a database of chess tactics puzzles. Even so, I thought the task would be a good illustration of some basic text processing techniques. This weekend I had such a task, although I must admit that it had nothing to do with engineering or science. It seems like a lot of computational tasks in engineering and science involve manipulating data in text files. Because this post is really about text processing. And that's about as close to image processing as today's blog post will come.
