Search code examples
imagematlabcell-array

how do solve this error message? 'Error using image Numeric or logical matrix required for image CDate


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?


Solution

  • 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