matlab bar 填充花纹,Matlab中画花纹填充的柱状图

在画柱状图时,利用不同的颜色区分在实际打印时经常区分不出。

为了能够更清楚第表示最终结果,可采用在柱状图中打阴影的方式来表示。

然而,Matalab本身没有提供实现这种功能的函数库。

下面两个函数:

% ApplyBrandon

's function

applyhatch_pluscolor

% Apply Brian'sfunction

applyhatch_plusC(fH,'\-x.', 'rkbk');

实现了上述功能。

这两个函数中都调用了函数makehatch_plus源码如下(都已经调试通过,可方便使用)。

h = bar(rand(3,4));

%随机生成3行4列的矩阵

fH = gcf;

colormap(jet(4));

%参数4表示4列数的颜色

legend('Apple', 'Orange', 'Banana','Melon', 'Location',

'EastOutside');

% Apply

Brandon'sfunction(PS:接着上面的代码写)

tH = title('Brandon''s applyhatch');

applyhatch_pluscolor(fH,'\-x.', 0, [1 0 1 0], jet(4));

%参数4可改变

% Apply Brian'sfunction

set(tH, 'String','Brian''s applyhatch');

applyhatch_plusC(fH,'\-x.', 'rkbk');

set(tH, 'String','Original');

代码源自:

http://www.mathworks.com/matlabcentral/fileexchange/24021-hatch-fill-patterns-plus-color-invert/content/applyhatch_pluscolor.m

function[im_hatch,colorlist] =

applyhatch_pluscolor(h,patterns,CvBW,Hinvert,colorlist,...

dpi,hatchsc,lw)

%APPLYHATCH_PLUSCOLORApply hatched patterns to a figure in

BW or Color

%  APPLYHATCH_PLUSCOLOR(H,PATTERNS)

creates anew figure from the figure H by

%  replacing distinct colors in H with

the blackand white

%  patterns in PATTERNS. The format for

PATTERNScan be

%    a

string of the characters:

%

'/', '\', '|', '-', '+', 'x', '.', 'c','w', 'k'

%

(see makehatch_plus.m for more details) or

%    a

cell array of matrices of zeros (white) andones (black)

%

%  In addition, H can alternatively be a

uint8NxMx3 matrix of the type

%  produced by imread.

In this case, colors in this image will be

%  replaced with patterns as if it was

afigure.  A final figure window

%  will be generated that displays

theresult.  The DPI argument

%  discussed below will be ignored if H

is animage matrix.

%

%  APPLYHATCH_PLUSCOLOR(H,PATTERNS,CVBW)

binaryvalue for choice of Color or Black

%  and White plots. If color is chosen

the colorwill match that of the

%  current fill. 1 ->

Color, anything else-> BW

%

%

APPLYHATCH_PLUSCOLOR(H,PATTERNS,CVBW,HINVERT)binary value to invert

the hatch.

%  i.e., if it is black lines with a

whitebackground, that becomes white

%  lines with a black background. This

can eitherbe a scalar value or a

%  1xN array equal to the length of

PATTERNS.When used as an array each

%  PATTERNS(i) will be inverted according

toHinvert(i). 1 -> Invert,

%  anything else -> Non

Inverted

%

% APPLYHATCH_PLUSCOLOR(H,PATTERNS,CVBW,HINVERT,COLORS)

maps the colors inthe n by 3

%  matrix COLORS to PATTERNS. Each row of

COLORSspecifies an RGB

%  color value.

%

%  Note this function makes a bitmap

image of Hand so is limited

%  to bitmap output.

%

%  Additional arguments:

%

%  [im_hatch,colorlist]

=applyhatch_plus(h,patterns,CvBW,Hinvert,colorlist,dpi,hatchsc,linewidth)

%

%

input  DPI

allows specificationof bitmap resolution, making plot

resolution

%

better forprinting.  Ignored if H is an

imagematrix.

%

HATCHSC    multiplier

for hatch scale to increase size of pattern for

betteroperation

%

at higher resolutions

%

default [] uses screenresolution as in

%

APPLYHATCH

%

LINEWIDTH   A scaling factor to

apply to line and dotsizes

%

in hatching.  Defaults to 1.

%

output IM_HATCH

RGB bitmap matrix ofnew figure

%

use IMWRITE to output indesired format

%

COLORLIST   List of colors

actually replaced.  Useful info if

%

no colorlist initiallygiven to function.

%

Colorlist will be uint8,not 0-1 scale as

%

originally specified.

%

%  Example 1:

%

bar(rand(3,4));

%

[im_hatch,colorlist]

=applyhatch_pluscolor(gcf,'\-x.',0,0,[],150);

%

imwrite(im_hatch,'im_hatch.png','png')

%

%  Example 2:

%

bar(rand(3,4));

%

[im_hatch,colorlist]

=applyhatch_pluscolor(gcf,'\-x.',1,[],[],150);

%

imwrite(im_hatch,'im_hatch.png','png')

%

%  Example 3:

%

colormap(cool(6));

%

pie(rand(6,1));

%   legend('Jan','Feb','Mar','Apr','May','Jun');

%

im_hatch =applyhatch_pluscolor(gcf,'|-.+\/',1,[1 1 0 1 0

0],cool(6),200,3,2);

%

imwrite(im_hatch,'im_hatch.png','png')

%

%  Example 4: Produces roughly the same

thing asexample 1

%

bar(rand(3,4));

%

print -dtiff -r150 im.tiff

%    im

= imread( 'im.tiff', 'tiff' );

%

[im_hatch,colorlist] =applyhatch_pluscolor(im,'\-x.');

%

imwrite(im_hatch,'im_hatch.tiff','tiff')

%

%

% Modification ofAPPLYHATCH to allow higher resolution

output

% Modified Brian

FGKatz

8-aout-03

% Modified David

MKaplan

19-fevrier-08

%

% Modification ofAPPLYHATCH_PLUS to allow for color and

inverted hatch

% ModifiedBrandon

Levey  May 6, 2009

%

%  See also: APPLYHATCH,

APPLYHATCH_PLUS,MAKEHATCH, MAKEHATCH_PLUS

%  By Ben Hinkle,

bhinkle@mathworks.com

%  This code is in the public

domain.

if~exist('CvBW','var'); CvBW =

0

;end  % defaults to black and white

if isempty(CvBW);CvBW =

0

; end  % defaults to black and white

if (CvBW ~= 0&& CvBW ~=

1); CvBW =

0

;end  % defaults to black and white

if~exist('Hinvert','var'); Hinvert =

0     ;

end  % defaults to not inverted

ifisempty(Hinvert); Hinvert =

0

; end  % defaults to not inverted

if length(Hinvert)== length(patterns) || length(Hinvert)

== 1

for

i = 1:length(Hinvert)

if Hinvert(i) ~= 0 &&Hinvert(i) ~=

1; Hinvert(i) =

0

; end

end

else

error(['The length of Hinvert must be 1 orequal to the length of

PATTERNS']);

end

if~exist('hatchsc','var'); hatchsc =

1     ;

end

if~exist('dpi','var'); dpi =

0

;end

% defaults to screen resolution

if~exist('colorlist','var'); colorlist =

[]  ; end

if~exist('lw','var'); lw=1; end

if numel(h) == 1 %Assume it is a figure window

oldppmode =

get(h,'paperpositionmode');

oldunits = get(h,'units');

oldcolor = get(h,'color');

oldpos = get(h,'position');

set(h,'paperpositionmode','auto');

set(h,'units','pixels');

set(h,'color',[1 1 1]);

figsize = get(h,'position');

bits =

hardcopy(h,'-dzbuffer',['-r'num2str(dpi)]);

% % Try a different approach using

atemporary file - use this if having probs

% tn = [ tempname '.tif' ];

% print( '-dtiff', [ '-r' num2str(dpi)

], tn)

% bits = uint8( imread( tn, 'TIFF' )

);

% delete(tn)

set(h,'paperpositionmode',oldppmode);

set(h,'color',oldcolor);

elseif size(h,3) ==3 % Assume it is an image

matrix

bits = h;

oldunits='pixels';

oldpos = [ 0, 0, size(bits,2),

size(bits,1)];

figsize = oldpos;

else

error( 'Bad first argument.'

);

end

bwidth =size(bits,2);

bheight =size(bits,1);

bsize = bwidth *bheight;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% The next bitbasically modernizes the original

% version of thisfunction using things like

unique

% and for loops

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Make bitmap onelong matrix with 3 columns

bits =reshape(bits,[bsize,3]);

% Convert originalcolor scale to 255 scale

if~isempty(colorlist)

% NOTE: Added "floor" below becausethis

seems to better pick out

% correct colors produced by

"hardcopyabove better than uint8 by itself

%colorlist =

uint8(255*colorlist);

colorlist =

uint8(floor(255*colorlist));

else

% Find unique colors in image - this

takes along time at high resolution

[B,I,J] = unique( bits, 'rows'

);

switch CvBW

case 0 % BW plot

% Find just "colored" colors

C = find( B(:,1)~=B(:,2) |B(:,1)~=B(:,3) );

case 1 % color plot

% Find all non black and white

B = sortrows(B);

C = 1:size(B,1);

C = C(2:end-1)';

end

colorlist = B( C , : );

end

% Loop over list ofcolors and find matches

for k =1:size(colorlist,1)

% Find points that match

color

if exist('B','var') % Use unique colors

ifaround

I =

C(k) == J;

else % Otherwise test each

point

cc

= colorlist(k,:);

I =

bits(:,1)==cc(1) &

bits(:,2)==cc(2)& bits(:,3)==cc(3);

if

~any(I(:)), continue, end

end

% What pattern to use

pati = mod( k-1, numel(patterns) ) +

1;

if iscell(patterns)

pattern = patterns{pati};

elseif isa(patterns,'char')

pattern =makehatch_plus(patterns(pati),6*hatchsc,lw);

else

pattern = patterns;

end

pattern = uint8(1-pattern);

if length(Hinvert) == 1

invertHatch = logical(Hinvert);

else

invertHatch = logical(Hinvert(pati));

end

% Make a big pattern matching size of

bits

pheight = size(pattern,2);

pwidth = size(pattern,1);

ratioh =

ceil(bheight/pheight);

ratiow =

ceil(bwidth/pwidth);

bigpattern = repmat(pattern,[ratioh

ratiow]);

if ratioh*pheight >

bheight

bigpattern(bheight+1:end,:) = [];

end

if ratiow*pwidth >

bwidth

bigpattern(:,bwidth+1:end) = [];

end

% Put that pattern into bits and

logicalvalues based on CvBW and Hinvert

switch CvBW

case 0 % BW

if invertHatch

bits(find(I),:) =repmat(~bigpattern(I)*255,[1,3]);

else

bits(find(I),:) =repmat(bigpattern(I)*255,[1,3]);

end

case

1 % Color

if invertHatch

bits(find(I),:) = [ ...

(uint8(bigpattern(I)) *colorlist(k,1)) + uint8((~bigpattern(I)) *

255), ...

(uint8(bigpattern(I)) *colorlist(k,2)) + uint8((~bigpattern(I)) *

255), ...

(uint8(bigpattern(I)) *colorlist(k,3)) + uint8((~bigpattern(I)) *

255)];

else

bits(find(I),:) = [ ...

(uint8(~bigpattern(I)) *colorlist(k,1)) + uint8((bigpattern(I)) *

255), ...

(uint8(~bigpattern(I))

*colorlist(k,2)) + uint8((bigpattern(I)) * 255), ...

(uint8(~bigpattern(I)) *colorlist(k,3)) + uint8((bigpattern(I)) *

255)];

end

end

end

% Put bits backinto its normal shape

bits = reshape(bits, [bheight,bwidth,3] );

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Replot

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

newfig

=figure('units',oldunits,'visible','off');

imaxes =axes('parent',newfig,'units','pixels');

im = image(bits,'parent',imaxes);

%fpos =get(newfig,'position');

%set(newfig,'position',[fpos(1:2)figsize(3)

figsize(4)+1]);

set(newfig,'position',oldpos)

set(newfig,'units','pixels')

set(imaxes,'position',[00 figsize(3)

figsize(4)+1],'visible','off');

set(newfig,'visible','on');

set(newfig,'units','normalized');

set(imaxes,'units','normalized');

set(imaxes,'DataAspectRatio',[11

1],'DataAspectRatioMode','manual');

if nargout > 0,im_hatch = bits;

end

if nargout < 2,clear colorlist;

end

(PS:函数结束)

代码源自:

http://www.mathworks.com/matlabcentral/fileexchange/24021-hatch-fill-patterns-plus-color-invert/content/makehatch_plus.m

function A =

makehatch_plus(hatch,n,m)

%MAKEHATCH_PLUSPredefined hatch patterns

%

% Modification ofMAKEHATCH to allow for selection of

matrix size. Useful whe using

%   APPLYHATCH_PLUS with

higher resolutionoutput.

%

% input

(optional)N

size of hatch matrix (default = 6)

% input

(optional)M

width of lines and dots in hatching(default = 1)

%

%  MAKEHATCH_PLUS(HATCH,N,M) returns a

matrixwith the hatch pattern for HATCH

%   according to the

following table:

%

HATCH

pattern

%

-------

---------

%

/

right-slanted lines

%

\

left-slanted lines

%

|

vertical lines

%

-

horizontal lines

%

+

crossing vertical and horizontallines

%

x

criss-crossing lines

%

.

square dots

%

c

circular dots

%

w

Just a blank white pattern

%

k

Just a totally black pattern

%

%  See also: APPLYHATCH,

APPLYHATCH_PLUS,APPLYHATCH_PLUSCOLOR, MAKEHATCH

%  By Ben Hinkle,

bhinkle@mathworks.com

%  This code is in the public

domain.

% Modified Brian

FGKatz

8-aout-03

% Modified David

MKaplan

19-fevrier-08

if~exist('n','var'), n = 6; end

if~exist('m','var'), m = 1; end

n=round(n);

switch (hatch)

case '\'

[B,C] = meshgrid( 0:n-1 );

B =

B-C;

clear C

A =

abs(B) <= m/2;

A =

A | abs(B-n) <= m/2;

A =

A | abs(B+n) <= m/2;

case '/'

A =

fliplr(makehatch_plus('\',n,m));

case '|'

A=zeros(n);

A(:,1:m) = 1;

case '-'

A =

makehatch_plus('|',n,m);

A =

A';

case '+'

A =

makehatch_plus('|',n,m);

A =

A | A';

case 'x'

A =

makehatch_plus('\',n,m);

A =

A | fliplr(A);

case '.'

A=zeros(n);

A(1:2*m,1:2*m)=1;

case 'c'

[B,C] = meshgrid( 0:n-1 );

A =

sqrt(B.^2+C.^2) <= m;

A =

A | fliplr(A) | flipud(A) |flipud(fliplr(A));

case 'w'

A =

zeros(n);

case 'k'

A =

ones(n);

otherwise

error(['Undefined hatch pattern "'hatch '".']);

end

代码源自

http://www.mathworks.com/matlabcentral/fileexchange/26797-applyhatchplusc/content/applyhatch_plusC.m

function im_hatchC=

applyhatch_plusC(h,patterns,patterncolors,colorlist,dpi,hatchsc)

%APPLYHATCH_PLUSCApply colored hatched patterns to a

figure

%  im_hatch

=applyhatch_plusC(h,patterns,patterncolors,colorlist,dpi,hatchsc)

%

%  APPLYHATCH_PLUSC(H,PATTERNS) creates a

newfigure from the figure H by

%  replacing distinct colors in H with

the blackand white

%  patterns in PATTERNS. The format for

PATTERNScan be

%    a

string of the characters '/', '\', '|','-', '+', 'x',

'.'

%    a

cell array of matrices of zeros (white)and ones (black)

%

%  By default the lines are of uniform

thickenss.hatch patterns line

%  thickness can be modified using a

direct callto MAKEHATCH_PLUS using

%  the following syntax:

makehatch_plus('HHn',m)where;

%

HH    thehatch character written

twice, '//', '\\', '||', '--', '++'

%

n  integer number for

thickness

%

m  integer number for the matrix

size (n<=m)

% Ex.makehatch_plus('\\4',9)

%

%  APPLYHATCH_PLUSC(H,PATTERNS,COLORS)

maps thecolors in the n by 3

%  matrix COLORS to PATTERNS. Each row of

COLORSspecifies an RGB

%  color value. COLORS can also be a

characterstring list.

%

%  Note this function makes a bitmap

image of Hand so is limited

%  to bitmap output.

%

%  Example 1: basic operation using color

charstring

%

bar(rand(3,6));

%

im_hatchC = applyhatch_plusC(1,'\-x.\x','rkgrgb');%

%

%  Example 2: basic operation using color

matrix

%

bar(rand(3,4));

%

im_hatchC = applyhatch_plusC(1,'\-x.',[10 0;0 1 0;0 0 1;0 1

1]);

%

%  Example 3: basic operation using

resolutionmodification

%

pie(rand(6,1));

%

legend('Jan','Feb','Mar','Apr','May','Jun');

%

im_hatch

=applyhatch_plusC(gcf,'|-+.\/','rgbcmy',[],150,0.5);

%

imwrite(im_hatch,'im_hatch.tiff','tiff')

%  Note : have not been able to

understandexactly how colors are assigned

%  for some plot functions, so better to

leaveCOLORLIST empty for

%  starters

%

%  Example 4: basic operation with user

definedpatterns

%

bar(rand(3,3));

%

im_hatch

=applyhatch_plusC(gcf,{makehatch_plus('\',6),1-makehatch_plus('\',6),makehatch_plus('\',1)},'ggg');%

%

% Example 5: usingvariable thickness hatches

%

bar(rand(3,3));

%

im_hatch

=applyhatch_plusC(gcf,{makehatch_plus('\',9),makehatch_plus('\\4',9),makehatch_plus('\\8',9)},'rgb');%

%

%  Example 6: basic operation using IMAGE

plot

%   data =

reshape([randperm(8) randperm(8)randperm(8)],4,6)

%

image(data)

%   im_hatch

=applyhatch_plusC(1,'|-+.\/x/','rgbcmykr',colormap);

% Note : do not useimagesc, as you need an indexed image

if you want to

% control the hatchassignments related to data

values.

%

% Modification ofAPPLYHATCH_PLUS to allow colored

patterns

% Modified Brian

FGKatz

25-feb-2010

%  im_hatch

=applyhatch_plusC(h,patterns,patterncolors,colorlist,dpi,hatchsc)

%

%

input  patterncolors

RGB matrix ofcolors for patterns

%

(length(PATTERNS)

X 3) orstring of color char

%

'r' 'g' 'b' 'c' 'm''y' of length = length(PATTERNS)

%

DPI

allows specification of bitmapresolution, making plot

resolution

%

better

for printing

%

HATCHSC

multiplier for hatch scale to increasesize of pattern for better

operation

%

at higherresolutions (not used when PATTERNS

%

defines pattern matrix)

%

default [] usesscreen resolution as in APPLYHATCH

%

output IM_HATCH

RGB bitmap matrixof new figure

%

use IMWRITE tooutput in desired format

%

% Modified Brian

FGKatz

21-sep-11

%   Variable line

thickness

%

%  See also: APPLYHATCH,

APPLYHATCH_PLUS

%  By Ben Hinkle,

bhinkle@mathworks.com

%  This code is in the public

domain.

oldppmode =get(h,'paperpositionmode');

oldunits =get(h,'units');

oldcolor =get(h,'color');

oldpos = get(h,'position');

set(h,'paperpositionmode','auto');

set(h,'units','pixels');

set(h,'color',[1 11]);

figsize =get(h,'position');

if nargin < 6;hatchsc =

1

; end

if nargin < 5;dpi =

0

;

end

% defaults to screen resolution

if nargin < 4;colorlist =

[]   ; end

if length(patterns)~= length(patterncolors)

error('PATTERN and PATTERNCOLORS must bethe same

length')

end

ifischar(patterncolors),

patterncolors =charcolor2rgb(patterncolors);

end

bits =hardcopy(h,'-dzbuffer',['-r'

num2str(dpi)]);

bitsC = ones(size(bits))*0;

blackpixels

=intersect(find(bits(:,:,1)==255),(intersect(find(bits(:,:,1)==bits(:,:,2)),find(bits(:,:,1)==bits(:,:,3))))

) ;

set(h,'paperpositionmode',oldppmode);

set(h,'color',oldcolor);

bwidth = size(bits,2);

bheight =size(bits,1);

bsize = bwidth *bheight;

if~isempty(colorlist)

colorlist =

uint8(floor(255*colorlist));

[colors,colori]

=nextnonbw(0,colorlist,bits);

else

colors = (bits(:,:,1) ~= bits(:,:,2)) |

...

(bits(:,:,1) ~=

bits(:,:,3));

end

pati = 1;

colorind =find(colors);

while~isempty(colorind)

colorval(1) =

bits(colorind(1));

colorval(2) =

bits(colorind(1)+bsize);

colorval(3) =

bits(colorind(1)+2*bsize);

if iscell(patterns)

pattern = patterns{pati};

elseif isa(patterns,'char')

pattern =makehatch_plus(patterns(pati),6*hatchsc);

else

pattern = patterns;

end

patternC =

uint8(255*pattern);

pattern =

uint8(255*(1-pattern));

pheight = size(pattern,2);

pwidth = size(pattern,1);

ratioh =

ceil(bheight/pheight);

ratiow =

ceil(bwidth/pwidth);

bigpattern = repmat(pattern,[ratioh

ratiow]);

if ratioh*pheight >

bheight

bigpattern(bheight+1:end,:) = [];

end

if ratiow*pwidth >

bwidth

bigpattern(:,bwidth+1:end) = [];

end

bigpattern = repmat(bigpattern,[1 1

3]);

% Create RGB pattern

pat_size = size(pattern,1)*size(pattern,2);

pat_id = find(patternC);

patternCrgb =repmat(ones(size(patternC))*255,[1 1

3]) ;

for

rgbLOOP = 1:3,

patternCrgb(pat_id+(pat_size*(rgbLOOP-1)))=patternCrgb(pat_id+(pat_size*(rgbLOOP-1)))*patterncolors(pati,rgbLOOP);

end

% rgbLOOP

bigpatternC = repmat(patternCrgb,[ratiohratiow 1]);

bigpatternC

=bigpatternC(1:size(bigpattern,1),1:size(bigpattern,2),:)

;

%

if ratioh*pheight > bheight

%

bigpatternC(bheight+1:end,:,:) = [];

%

end

%

if ratiow*pwidth > bwidth

%

bigpatternC(:,bwidth+1:end,:) = [];

%

end

color = (bits(:,:,1) == colorval(1))

&...

(bits(:,:,2) == colorval(2)) &

...

(bits(:,:,3)== colorval(3));

color = repmat(color,[1 1

3]);

bits(color) =

bigpattern(color);

bitsC(color) = bigpatternC(color);

if ~isempty(colorlist)

[colors,colori] =nextnonbw(colori,colorlist,bits);

else

colors = (bits(:,:,1) ~= bits(:,:,2)) | ...

(bits(:,:,1)

~= bits(:,:,3));

end

colorind = find(colors);

pati = (pati + 1);

if pati >

length(patterns)

pati = 1;

end

end

bitsC(blackpixels)=255;

bitsC(blackpixels+(bheight*bwidth))=255;

bitsC(blackpixels+(2*(bheight*bwidth)))=255;

newfig

=figure('units','pixels','visible','off');

imaxes =axes('parent',newfig,'units','pixels');

im =image(bitsC/255,'parent',imaxes);

%fpos =get(newfig,'position');

%set(newfig,'position',[fpos(1:2)figsize(3)

figsize(4)+1]);

set(newfig,'position',oldpos)

set(imaxes,'position',[00 figsize(3)

figsize(4)+1],'visible','off');

set(newfig,'visible','on');

set(newfig,'units','normalized');

set(imaxes,'units','normalized');

set(imaxes,'DataAspectRatio',[11

1],'DataAspectRatioMode','manual');

if nargout

==1,

im_hatchC = bitsC; end

function[colors,out] = nextnonbw(ind

,colorlist,bits)

out =ind

+1;

colors = [];

while out <=size(colorlist,1)

if isequal(colorlist(out,:),[255 255

255]) |...

isequal(colorlist(out,:),[0 0 0])

out

= out+1;

else

colors = (colorlist(out,1) == bits(:,:,1))&

...

(colorlist(out,2)

== bits(:,:,2)) & ...

(colorlist(out,3)

== bits(:,:,3));

return

end

end

function colors_rgb=

charcolor2rgb(colors_char);

forLOOP

= 1:length(colors_char),

switch colors_char(LOOP

)

case 'r'

colors_rgb(LOOP,:) = [1 0

0]

;

case 'g'

colors_rgb(LOOP,:) = [0 1

0]

;

case 'b'

colors_rgb(LOOP,:) = [0 0

1]

;

case 'c'

colors_rgb(LOOP,:)

= [0 1 1]

;

case 'm'

colors_rgb(LOOP,:) = [1 0

1]

;

case 'y'

colors_rgb(LOOP,:) = [1 1

0]

;

case 'k'

colors_rgb(LOOP,:) = [0 0

0]

;

otherwise

error('Invalid folor char

string')

end

end

转自:http://www.doc88.com/p-6771119624572.html​

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值