0

I have the following code:

tile {6} = imread ('tw.png','png');
tile {5} = imread ('twpw.png','png');
tile {4} = imread ('twpb.png','png');
tile {3} = imread ('tb.png','png');
tile {2} = imread ('tbpw.png','png');
tile {1} = imread ('tbpb.png','png');

board = zeros(8,8)

% the board image matrix is first empty (no tile )
board = [];

% we add four empty white tiles to the first row
board = [ board tile {6} tile {6} tile {6} tile {6}];

% we add four black tiles with white pawn to the second row
board = [ board ; tile {2} tile {2} tile {2} tile {2}];

figure(1);
image(board);
set (gcf ,'Position ' ,[150 150 50* c 50* r]);

But I get the error:

Error using image.

Numeric or logical matrix required for image CData

Why can't my board show up?

Unihedron
  • 10,902
  • 13
  • 62
  • 72
Dickson tan
  • 35
  • 2
  • 8

1 Answers1

1

You cannot have a space in the expression tile {i}, it should be tile{i}.

Otherwise it interpreted as tile (the entire cell array), then {i} (a cell array containing a scalar value), rather than indexing into the array using tile{i} to get the i-th cell content.


EDIT:

Here is some code to illustrate:

% cell-array of tiles
tile{1} = zeros(5,5);    % black tile
tile{2} = ones(5,5);     % white tile
tile{3} = ones(5,5)*0.5; % gray tile

% build a 2x3 matrix of "blocks"
board = [tile{2}, tile{1}, tile{2} ;
         tile{1}, tile{3}, tile{1}];

% show as indexed image with a grayscale colormap
imagesc(board), axis image
colormap(gray(3))
h = colorbar; set(h, 'YTick',[0 0.5 1])

tiled_image

Amro
  • 123,847
  • 25
  • 243
  • 454
  • if i want to change the white tile into black tile? how do i access the white tile and make it into black? – Dickson tan Oct 15 '14 at 15:54
  • i got this error Undefined function 'zero' for input arguments of type 'double'. when i tried the 'tile{1} = zeros(5,5);' – Dickson tan Oct 15 '14 at 15:56