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
%
% Options for 1-dim:
%       'height',val        scalar or 1x2 vector, default=[0 1],
%                           determines the length of the lines to be plotted
%                               scalar: Line goes from 0 to val
%                               vector: Line goes from val(1) to val(2) 
%
% Options for 3-dim:
%       'resolution',val    scalar, default depends on the input
%                           determines the resolution
%                               If val==0, the points are plotted
%                               otherwise val determines the resolution of the interpolated grid
%
% 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.: clf; hold on; clear all; load trimesh3d; plotm([x y z]','resolution',500); plotm([x y z]','resolution',0);
%       plotm(randn(2,40),'.-')
%       plotm(randn(1,40),'height',4)
%
% 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 )
    if(isreal(Q))
        try
            [height,varargin]=parsem('height',varargin,1);
            [~,varargin]=parsem('MarkerSize',varargin, 0); %Remove MarkerSize
            [~,varargin]=parsem( 'resolution', varargin, 0 ); %#ok<ASGLU> %Remove resolution
        catch
            height=[0 1];
        end
        if(numel(height)==1); height=[0 height]; end;
        ONE=ones(1,size(Q,2));
        ZERO=zeros(1,size(Q,2));
        try
            quiver( Q, height(1)*ONE, ZERO, (height(2)-height(1))*ONE, 'AutoScale','off','ShowArrowHead','off', 'Marker', 'none', 'LineWidth', .01); 
        catch
            fprintf('Error during plotm. All options are ignored.\n');
            stem( Q, ones(1,size(Q,2))); 
        end
    else
        plotm([real(Q); imag(Q)],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
    if(xres==0)
        %plot3(x,y,z,'.',varargin{:})
        [COLOR,varargin]=parsem('Color',varargin,[0 0 0]);
        [MARKERSIZE,varargin]=parsem('MarkerSize',varargin,5);
        scatter3(x,y,z,MARKERSIZE,COLOR,'.',varargin{:})
    else
        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
    
end




% OLD PLOT CODE
% if dim_inner==3;
%     c=padarraym(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.