function plotm( varargin)
% plotm( data, [options])
% Wrapper function which calls stem/plot/plot3 depending on the dimension of varargin{1}
%
% Input:
%   data        dim x N array
%   [options]   everything which can be parsed by plot/plot2/plot3
%               if dim=1, stem is called instead of plot
%
% Info:
%   1-dim data:
%       Each point is plotted as a dot at height 1. 
%       The height can be changed by Option <'height',val>
%       To change the marker, the option <'Marker','something'> should be used
%
%   2-dim/3-dim data 
%       A point cloud is plotted
%
% E.g.: plotm(randn(2,40),'.-')
%       plotm(randn(1,40))
%
% See also: surfm, plot, plot3, stem
%
% Written by: tommsch, 2016

% XX Write plotm4, using the commented out code below

dim=size(varargin{1},1);
if(dim==1); plotm1(varargin{:});
elseif(dim==2); plotm2(varargin{:}); 
elseif(dim==3); plotm3(varargin{:});
else; fprintf('no ''plotm'' for dim>3\n');
end;

end

function plotm1( Q, varargin )
    try
        [height,varargin]=parsem('height',varargin,1);
    catch
        height=1;
    end
    try
        stem( Q, height*ones(1,size(Q,2)), 'Marker', 'none', 'LineWidth', .01, varargin{:} ); 
    catch
        stem( Q, height*ones(1,size(Q,2)), varargin{:} ); 
    end
end

function plotm2( Q , varargin)
    try
        plot(Q(1,:),Q(2,:),varargin{:});
    catch
         plot(Q(1,:),Q(2,:));
    end
    xlabel('x')
    ylabel('y')
    zlabel('z')
end

function plotm3( Q , varargin)
    x=Q(1,:).';
    y=Q(2,:).';
    z=Q(3,:).';
    Q=[]; %#ok<NASGU> %free space
    minx=min(x); maxx=max(x);
    miny=min(y); maxy=max(y);
    try
        [xres,~]=       parsem( 'resolution', varargin, max(33,ceil(sqrt(length(x)))) );
        [yres,varargin]=parsem( 'resolution', varargin, max(33,ceil(sqrt(length(y)))) );
    catch
        xres=max(33,ceil(sqrt(length(x))));
        yres= max(33,ceil(sqrt(length(y))));
    end
    xlin = linspace(minx,maxx,xres);
    ylin = linspace(miny,maxy,yres);
    [X,Y] = meshgrid(xlin,ylin);
    f = scatteredInterpolant(x,y,z,'linear','none'); %linear interpolation, none extrapolation
    Z = f(X,Y);
    try
        mesh(X,Y,Z,varargin{:}) %interpolated
    catch
        mesh(X,Y,Z) %interpolated
    end
    axis tight;
end




% OLD PLOT CODE
% if dim_inner==3;
%     c=padarray(c,[1 1 1]); %add zeros at the boundary, otherwise the graphics look bad
%     [y,x,z]=size(c);
%     MIN=min(min(min(c)));
%     MAX=max(max(max(c)));
%     x=1:x;y=1:y;z=1:z;
% 
%     clf;
%     hold on;
% 
%     if numel(c)>250000; 
%         p0=patch( isosurface(x,y,z,c,0.99*MAX+0.01*MIN)); isonormals(x,y,z,c, p0); set(p0,'FaceColor','black', 'EdgeColor','none','FaceAlpha',0.7);
%         p5=patch( isosurface(x,y,z,c,0.01*MAX+0.99*MIN)); isonormals(x,y,z,c, p5); set(p5,'FaceColor','blue',  'EdgeColor','none','FaceAlpha',0.2);
%     else
%         p0=patch( isosurface(x,y,z,c,0.99*MAX+0.01*MIN)); isonormals(x,y,z,c, p0); set(p0,'FaceColor','black', 'EdgeColor','none','FaceAlpha',0.7);
%         p1=patch( isosurface(x,y,z,c,0.80*MAX+0.20*MIN)); isonormals(x,y,z,c, p1); set(p1,'FaceColor','red',   'EdgeColor','none','FaceAlpha',0.6);
%         p2=patch( isosurface(x,y,z,c,0.60*MAX+0.40*MIN)); isonormals(x,y,z,c, p2); set(p2,'FaceColor','yellow','EdgeColor','none','FaceAlpha',0.5);
%         p3=patch( isosurface(x,y,z,c,0.40*MAX+0.60*MIN)); isonormals(x,y,z,c, p3); set(p3,'FaceColor','cyan',  'EdgeColor','none','FaceAlpha',0.4);
%         p4=patch( isosurface(x,y,z,c,0.20*MAX+0.80*MIN)); isonormals(x,y,z,c, p4); set(p4,'FaceColor','green', 'EdgeColor','none','FaceAlpha',0.3);
%         p5=patch( isosurface(x,y,z,c,0.01*MAX+0.99*MIN)); isonormals(x,y,z,c, p5); set(p5,'FaceColor','blue',  'EdgeColor','none','FaceAlpha',0.2);
%     end;
%     view(3); camlight; lighting phong;
%     hold off;
% end

function dummy; end %#ok<DEFNU> %Generates an error, if the 'end' of a function is missing.