MATLAB: Converting cell of strings to matrix of numbers for Chess Program (and java heap error!)

arraycellstring

I'm programming a chess game and I've got a cell of strings which the GUI uses to draw the boards, but want to convert this into an array for the calculations.
tmp = {...
'rook:2','knight:2','bishop:2','queen:2','king:2','bishop:2','knight:2','rook:2';...
'pawn:2','pawn:2','pawn:2','pawn:2','pawn:2','pawn:2','pawn:2','pawn:2';...
'none:0','none:0','none:0','none:0','none:0','none:0','none:0','none:0';...
'none:0','none:0','none:0','none:0','none:0','none:0','none:0','none:0';...
'none:0','none:0','none:0','none:0','none:0','none:0','none:0','none:0';...
'none:0','none:0','none:0','none:0','none:0','none:0','none:0','none:0';...
'pawn:1','pawn:1','pawn:1','pawn:1','pawn:1','pawn:1','pawn:1','pawn:1';...
'rook:1','knight:1','bishop:1','queen:1','king:1','bishop:1','knight:1','rook:1' };
What I want is a matrix that looks like:
[5 3 3 9 200 3 3 5...
1 1 1 1 1 1 1 1...
etc.
]
Where the numbers correspond to the relative value of the pieces. And I also need to be able to change these back again to a cell of strings.
Additionally, the reason for doing this is that at the moment I keep on getting "failed to retrieve exception message" if I look two steps ahead. The size of tmp is 7938mb and there are four additional versions of it which need to be constantly updated, so I wondered if this is what was causing the java heap storage to fail. I have already increased the storage in the .opts file to as much as I can, as well as downloading and using the jheapcl function and still no luck.
If you can help at all with either queries it would be much appreciated!
Thanks in advance,
Chris

Best Answer

It doesn't look like the colour of the pieces matter for your numerical matrix, so you would be better off storing that in a matrix or cell array separate to the pieces name. As it is, the first thing is to get rid of that information:
boardpieces = cellfun(@(e) regexp(e, '[^:]+', 'match', 'once'), tmp, 'UniformOutput', false)
You can then use the second output of ismember to index into a matrix of values:
pieces = {'none', 'pawn', 'knight', 'bishop', 'rook', 'queen', 'king'};
values = [0 1 3 3 5 9 200];
[~, idx] = ismember(boardpieces, pieces);
boardvalue = values(idx);