Code covered by the BSD License  

Highlights from
UIMAGE - UIMAGESC

4.88889

4.9 | 9 ratings Rate this file 23 Downloads (last 30 days) File Size: 3.36 KB File ID: #11368
image thumbnail

UIMAGE - UIMAGESC

by Frederic Moisy

 

12 Jun 2006 (Updated 30 Jan 2012)

Display image with non-linearly spaced axis.

| Watch this File

File Information
Description

UIMAGE(X,Y,C) displays matrix C as an image, using the vectors X and Y to specify the X and Y coordinates. X and Y may be unevenly spaced vectors, but must be increasing. The size of C must be LENGTH(Y)*LENGTH(X). (Most probably you'll want to display C' instead of C).
 
Contrary to Matlab's original IMAGE function, here the vectors X and Y do not need to be linearly spaced. Whereas Matlab's IMAGE function linearly interpolates the X-axis between X(1) and X(end), ignoring all other values (idem for Y), here UIMAGE allows for X and/or Y to be unevenly spaced vectors, by locally stretching the matrix C (ie, by duplicating some elements of C) for larger X and/or Y intervals.

Use UIMAGESC to scale the data using the full colormap. The syntax for UIMAGESC(X,Y,C,...) is the same as IMAGESC(X,Y,C,...).
 
Typical uses:
- Plotting a spatio-temporal diagram (T,X), with unevenly spaced time intervals for T (eg, when some values are missing, or when using a non-constant sampling rate).
- Plotting a set of power spectra with frequency in log-scale.

Example:
 c = randn(50,20); % Random 50x20 matrix
 x = logspace(1,3,50); % log-spaced X-axis, between 10 and 1000
 y = linspace(3,8,20); % lin-spaced Y-axis, between 3 and 8
 uimagesc(x,y,c'); % displays the matrix

MATLAB release MATLAB 7.2 (R2006a)
Tags for This File  
Everyone's Tags
plotting, specialized, uimagesc image uneven non linear axis
Tags I've Applied
Add New Tags Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (15)
03 Jul 2012 natan

very nice work, should be part of Matlab in my view...

08 May 2012 Sven

Secondly, imagine this scenario:

You have a volume of images, and you want to scroll through those images, viewing each one. If they were monotonically spaced, you could just call h=image(X,Y,C(:,:,1)) on the first slice, then call set(h,'CData',C(:,:,i)) on the rest of the slices.

When they are NOT monotonically spaced, you now need to call h=uimage(X,Y,C(:,:,1)). The problem here is that the 'CData' has a new number of rows/columns, so for subsequent images you can't just update 'CData' like you could before.

Instead, consider if uimage also gave the *indices* it used to resample C monotonically. Now on the first image you could call:
[h,Xinds,Yinds]=uimage(X,Y,C(:,:,1))
and on subsequent calls you could now use:
set(h,'CData',C(Yinds,Xinds,i))

This would save re-calculating the resampling locations for each separate slice (since they are all the same), and it would also save deleting and re-creating image handles for each slice you want to show.

Frederic, I've just sent you an overhaul of your code that does just this (and includes the speed-up from my above comment).

08 May 2012 Sven

There are a few places where big speed gains can be made. Firstly, the interpolation step can be improved by removing a loop near line 88:

tic % Original version
for j=1:ny
indj = find(y<=ye(j));
ce(j,1:nx) = c(indj(end), 1:nx);
end
toc
tic % Faster version
T = bsxfun(@minus, ye(:), y(:)')>=0;
% Following two lines can be replaced with FIND_NDIM: indsJ = find_ndim(T,2,'last');
[~, indsJ] = max(flipdim(T,2),[],2);
indsJ = length(y)+1 - indsJ;
ce2 = c(indsJ,:);
toc
isequal(ce, ce2)

Elapsed time is 0.013454 seconds.
Elapsed time is 0.001384 seconds.

ans =

1

So here we get a 13x speedup (and the sped up version gives identical output to the original)

06 Feb 2012 Oliver Woodford

pcolor creates patch objects, which can create problems when exporting using the painters renderer. This method allows images with non-linear axes to be rendered as images, which export much better.

06 Feb 2012 Oliver Woodford  
31 Jan 2012 Rob Campbell

It's still a good idea. It's just a redundant good idea ;)

31 Jan 2012 Frederic Moisy

Oooooooh... I must admit that Bjorn Gustavsson is totally right. This submission actually mimick to behavior of pcolor. Thank you for your remark, and sorry for this false good idea...
F.

31 Jan 2012 Bjorn Gustavsson

Just a question: What improved features does this provide compared to simply calling:

pcolor(x,y,c),shading flat,axis ij

19 Jan 2012 Tim

I just debugged an error similar to that of Janti.... I think the i,j indexing is wrong in line 122. See below..

% ce(j,i) = c(indi(end), indj(end)); % original version
ce(j,i) = c(indj(end), indi(end)); % "corrected" version

14 Jun 2011 Janti

If you need to have both axis in logspace you get an error.

please add the following in the for loop in uimage.m line:120

indi = find(x<=xe(i));
indi = min(indi,length(y));
indj = find(y<=ye(j));
indj = min(indj,length(x));
ce(j,i) = c(indi(end), indj(end));

Thanks for this m file!

15 Jun 2010 Jon-Fredrik Hopperstad

Excellent work. This was exactly what I was looking for. I just did 15 query-replace for 'imagesc' in my code and it works beautifully. Thanks!

24 Aug 2009 Gabriel Akira Schreiber

Oh soo cool! Just what I needed.Very nicely done.

28 Mar 2008 Dhanya parameshwaran

Thanks Frederic. Clever trick , works perfectly!

09 Mar 2008 Sven Holcombe

Excellent work. Piggybacks neatly on core matlab image() and imagesc() functions so I can simply replace these calls. If my input is in fact perfectly linear, then these calls are used directly without further processing. Thank you.

13 Jun 2006 RALIHALIZARA Julliard

BEautifull idea!

Updates
13 Jun 2006

help text improved

05 Sep 2006

submissions UIMAGE and UIMAGESC
are now merged into a single zip
file.

30 Jan 2012

Bug fixed when both X and Y axis are not linearly spaced (thank you Janti and Tim!!)

Contact us