function HH=linpat(H)
%LINe to PATch and/or PATch to LINe conversion
%Call:
% HH=linpat([H])
%Input:
% H = handle(s) or descriptor of the objects to be converted
% By default, manual selection by click(s):
% selection is finished by click ouside of axis limits
% H as descriptor can be one of the three strings:
% 'all' - ALL lines and patches selected
% 'lin' - all LINes selected
% 'pat' - all PATches selected
%Output:
% HH is(are) handle(s) to converted objects:
% lines are converted to patches (color-->FaceColor),
% patches are converted to lines (FaceColor-->color)
%
% LINPAT demos
% 1. Convert manually selected lines/patches
% linpat
% 2. Convert all lines to patches and all patches to lines:
% HH=linpat('all')
% 3. Convert all patches to lines:
% HH=linpat('pat')
% 4. Convert all lines to patches:
% HH=linpat('lin')
% 5. Convert predefined objects H:
% HH=linpat(H)
% 6. Application similar to that in the screenshot (optional)
%f=figure;
%h=mlin('2r');
%set(h(2:2:end),'color','b')
%ff=figure;
%subplot(1,2,1);
%copyobj(h,gca);
%subplot(1,2,2);
%hh=copyobj(h,gca);
%c=linpat(hh);
% Vassili Pastushenko DEC 2007
%==============================
NAR=nargin;
currax=gca;
axis(axis);
switch NAR
case 0 %manual selection
xx=xlim;
yy=ylim;
H=[];
ht=text(mean(xx),yy(2)+.06*diff(yy),'Select (click) line(s)/patche(s). Convert = click outside of xylims');
set(ht,'fontsize',15,'Backgroundcolor','r','HorizontalAlignment','center');
drawnow
coun=0;
while 1
coun=coun+1;
waitforbuttonpress
cup=get(currax,'Currentpoint');
if prod(cup(1)-xlim)>0||prod(cup(3)-ylim)>0
break
end
curobj=gco;
Type=get(curobj,'type');
LIN=~isempty(strmatch(Type,'line','exact'));
PAT=~isempty(strmatch(Type,'patch','exact'));
if LIN||PAT
H(coun)=curobj;
set(H(coun),'Selected','on');
else
coun=coun-1;
cheer
warning('Not line or patch')
end
end %while
set(H,'selected','off');
delete(ht) %Finish manual selection
case 1 %if descriptor selection
if strmatch(class(H),'char','exact')
Hlin=findobj(currax,'Type','line');
Hpat=findobj(currax,'Type','patch');
LIN=strmatch(H,'lin','exact');
PAT=strmatch(H,'pat','exact');
ALL=strmatch(H,'all','exact');
if LIN, H=Hlin; end
if PAT, H=Hpat; end
if ALL, H=[Hlin(:);Hpat(:)]; end
end %Finish descriptor selection
case 2
error('Too many inputs')
end %end %switch
% Convert (redefines) selected objects
HH=[]; %Initialize output
for i=1:numel(H)
Type=get(H(i),'Type');
LIN=~isempty(strmatch(Type,'line','exact'));
PAT=~isempty(strmatch(Type,'patch','exact'));
if ~LIN&&~PAT
cheer;
warning('not LINE or PATCH')
H(i)=NaN;
continue
end
x=get(H(i),'XData');
y=get(H(i),'YData');
if LIN
Col=get(H(i),'color');
HH(i)=patch(x,y,Col,'FaceAlpha',.8);
elseif PAT
Col=get(H(i),'FaceColor');
HH(i)=line(x,y,'color',Col,'linestyle','-','marker','.');
else
cheer;
warning('Not LINE or PATCH')
end
end
if ~isempty(H)
H(isnan(H))=[];
delete(H);
cheer(numel(H));
end
HH(HH==0)=[];
function cheer(N)
if nargin<1
N=3;
end
s=[];
M=round(1000*unifrnd(.6,1,1,N));
for i=1:N
wid=M(i);
x=(0:wid)';
s=[s;sin((pi/wid)*x).^3.*sin(2*(x+x.^2/M(i)))];
end
sound(s/2);