matthiasm@8: function h = uimage(varargin) matthiasm@8: %UIMAGE Display image with uneven axis. matthiasm@8: % UIMAGE(X,Y,C) displays matrix C as an image, using the vectors X and matthiasm@8: % Y to specify the X and Y coordinates. X and Y may be unevenly spaced matthiasm@8: % vectors, but must be increasing. The size of C must be LENGTH(Y)* matthiasm@8: % LENGTH(X). (Most probably you'll want to display C' instead of C). matthiasm@8: % matthiasm@8: % Contrary to Matlab's original IMAGE function, here the vectors X and Y matthiasm@8: % do not need to be linearly spaced. Whereas IMAGE linearly interpolates matthiasm@8: % the X-axis between X(1) and X(end), ignoring all other values (idem matthiasm@8: % for Y), UIMAGE allows for X and/or Y to be unevenly spaced vectors, by matthiasm@8: % locally stretching the matrix C (ie, by duplicating some elements of C) matthiasm@8: % for larger X and/or Y intervals. matthiasm@8: % matthiasm@8: % The syntax for UIMAGE(X,Y,C,...) is the same as IMAGE(X,Y,C,...) matthiasm@8: % (all the remaining arguments, eg 'PropertyName'-PropertyValue pairs, matthiasm@8: % are passed to IMAGE). See IMAGE for details. matthiasm@8: % matthiasm@8: % Use UIMAGESC to scale the data using the full colormap. The syntax for matthiasm@8: % UIMAGESC(X,Y,C,...) is the same as IMAGESC(X,Y,C,...). matthiasm@8: % matthiasm@8: % Typical uses: matthiasm@8: % - Plotting a spatio-temporal diagram (T,X), with unevenly spaced matthiasm@8: % time intervals for T (eg, when some values are missing, or when matthiasm@8: % using a non-constant sampling rate). matthiasm@8: % - Plotting a set of power spectra with frequency in log-scale. matthiasm@8: % matthiasm@8: % h = UIMAGE(X,Y,C,...) returns a handle to the image. matthiasm@8: % matthiasm@8: % Example: matthiasm@8: % c = randn(50,20); % Random 50x20 matrix matthiasm@8: % x = logspace(1,3,50); % log-spaced X-axis, between 10 and 1000 matthiasm@8: % y = linspace(3,8,20); % lin-spaced Y-axis, between 3 and 8 matthiasm@8: % uimagesc(x,y,c'); % displays the matrix matthiasm@8: % matthiasm@8: % F. Moisy matthiasm@8: % Revision: 1.03, Date: 2006/06/14. matthiasm@8: % matthiasm@8: % See also IMAGE, IMAGESC, UIMAGESC. matthiasm@8: matthiasm@8: matthiasm@8: % History: matthiasm@8: % 2006/06/12: v1.00, first version. matthiasm@8: % 2006/06/14: v1.03, minor bug fixed; works in ML6. matthiasm@8: matthiasm@8: error(nargchk(3,inf,nargin)); matthiasm@8: matthiasm@8: % maximum number of matrix elements to interpolate the uneven axis matthiasm@8: % (typically between 500 and 5000): matthiasm@8: nmax = 2000; matthiasm@8: matthiasm@8: x = varargin{1}; matthiasm@8: y = varargin{2}; matthiasm@8: c = varargin{3}; matthiasm@8: matthiasm@8: if any(diff(x)<=0) || any(diff(y)<=0) matthiasm@8: error('The X and Y axis should be increasing.'); matthiasm@8: end matthiasm@8: matthiasm@8: dx = min(diff(x)); % smallest interval for X matthiasm@8: dy = min(diff(y)); % smallest interval for Y matthiasm@8: matthiasm@8: % test if X and Y are linearly spaced (to within 10^-12): matthiasm@8: evenx = all(abs(diff(x)/dx-1)<1e-12); % true if X is linearly spaced matthiasm@8: eveny = all(abs(diff(y)/dy-1)<1e-12); % true if Y is linearly spaced matthiasm@8: matthiasm@8: matthiasm@8: if evenx && eveny % X and Y both evenly spaced matthiasm@8: matthiasm@8: xe = x; matthiasm@8: ye = y; matthiasm@8: ce = c; matthiasm@8: matthiasm@8: elseif evenx && ~eveny % X even and Y uneven matthiasm@8: matthiasm@8: nx = length(x); matthiasm@8: xe = x; matthiasm@8: matthiasm@8: ny = ceil(1 + (y(end) - y(1))/dy); % number of points for Y matthiasm@8: ny = min(ny, nmax); matthiasm@8: ye = linspace(y(1), y(end), ny); matthiasm@8: matthiasm@8: ce = zeros(ny,nx); matthiasm@8: matthiasm@8: for j=1:ny matthiasm@8: indj = find(y<=ye(j)); matthiasm@8: ce(j,1:nx) = c(indj(end), 1:nx); matthiasm@8: end; matthiasm@8: matthiasm@8: elseif ~evenx && eveny % X uneven and Y even matthiasm@8: matthiasm@8: nx = ceil(1 + (x(end) - x(1))/dx); % number of points for X matthiasm@8: nx = min(nx, nmax); matthiasm@8: xe = linspace(x(1), x(end), nx); matthiasm@8: matthiasm@8: ny = length(y); matthiasm@8: ye = y; matthiasm@8: matthiasm@8: ce = zeros(ny,nx); matthiasm@8: matthiasm@8: for i=1:nx matthiasm@8: indi = find(x<=xe(i)); matthiasm@8: ce(1:ny,i) = c(1:ny, indi(end)); matthiasm@8: end; matthiasm@8: matthiasm@8: elseif ~evenx && ~eveny % X and Y both uneven matthiasm@8: matthiasm@8: nx = ceil(1 + (x(end) - x(1))/dx); % number of points for X matthiasm@8: nx = min(nx, nmax); matthiasm@8: xe = linspace(x(1), x(end), nx); matthiasm@8: matthiasm@8: ny = ceil(1 + (y(end) - y(1))/dy); % number of points for Y matthiasm@8: ny = min(ny, nmax); matthiasm@8: ye = linspace(y(1), y(end), ny); matthiasm@8: matthiasm@8: ce = zeros(ny,nx); matthiasm@8: matthiasm@8: for i=1:nx matthiasm@8: for j=1:ny matthiasm@8: indi = find(x<=xe(i)); matthiasm@8: indj = find(y<=ye(j)); matthiasm@8: ce(j,i) = c(indi(end), indj(end)); matthiasm@8: end; matthiasm@8: end; matthiasm@8: matthiasm@8: end matthiasm@8: matthiasm@8: hh = image(xe, ye, ce, varargin{4:end}); matthiasm@8: matthiasm@8: if nargout>0 matthiasm@8: h = hh; matthiasm@8: end