Why does FILL give me dimension errors when my inputs are grouped, such as fill([x1, x2], [y1,y2],[c1,c2]) in MATLAB 7.7 (R2008b)?

2 views (last 30 days)
I want to use FILL to color two rectangles – having their ‘x’ and ‘y’ coordinates grouped in ‘X’ and ‘Y’, and their colors as '[r g b]' grouped in C. I get an error when I execute:
x = [0 1 1 0; 1 4 4 1]; % x coordinates for two rectangles
y = [0 0 2 2; 0 0 2 2]; % y coordinates for two rectangles
c = [1 0 0; 0 0 1]; % red for one rectangle and blue for the other
fill(x,y,c)
??? Error using ==> fill
Size of C must match sizes of X Y [Z]

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 23 Aug 2010
Grouped inputs can be used for FILL in MATLAB 7.7 (R2008b).
To use FILL for multiple polygons with different colors, the syntax is:
>> fill(X1,Y1,C1,X2,Y2,C2,...),
in which ‘X1’ and ‘Y1’ define the coordinates of the first rectangle and ‘C1’ defines the color as ‘[r g b]’ or ‘colorstring’. If you group the X and Y coordinates as well as colors for two rectangles before calling FILL, they need to be comma-separated by converting matrices to CELL so that X1, Y1, C1, X2, Y2, and C2 can be called. The following MATLAB code provides an example:
% Assuming you have rectangles defined in "x", "y" and colors in "c"
x = [0 1 1 0; 1 4 4 1];
y = [0 0 2 2; 0 0 2 2];
c = [1 0 0; 0 0 1];
% Convert to cell array and rearrange appropriately
% To return output of cell arrays X, Y, and C, execute X{:}, Y{:}, and C{:}
X = num2cell(x, 2);
Y = num2cell(y, 2);
C = num2cell(c, 2);
args = [X Y C]';
args = args(:);
% "args{:}" expands to comma-separated list, hence you can call FILL directly
fill( args{:} )

More Answers (0)

Categories

Find more on Software Development Tools in Help Center and File Exchange

Tags

Products


Release

R2008b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!