Date: Jul 18, 2012 9:07 PM
Author: spasmous
Subject: This function crashes matlab occasionally... why?!

I have a function that displays several images in one figure (see below). I've also been experiencing a lot of MATLAB/Java crashes that I think can be traced to this function. I'm not sure why it happens... it's quite rare, once a week or two. Can someone tell me what is causing the problem?


q=repmat(phantom(256,256),[1 1 9]);
ims(q)



function ims(im,RANGE,TITLE)

% RANGE: e.g. [0 1] for [hi lo] or [0 1;NaN 100;...] for individual ranges
% (note: use [] or NaN for no range)
% TITLE: ['plot1';'plot2';...] (must be same length)

% make into 3D array
im = squeeze(im);
[x y n] = size(im);
im = reshape(im,x,y,n);

% try and figure out a nice tiling
rows = floor(sqrt(n));
cols = ceil(n/rows);
N = [rows cols];

% clear existing plots
clf reset

% plot here
for i = 1:N(1)
for j = 1:N(2)
k = (i-1)*N(2)+j;
if k>n; break; end

lo = min(min(im(:,:,k)));
hi = max(max(im(:,:,k)));

if exist('RANGE','var') && ~isempty(RANGE)
if size(RANGE,1)==1 % use same setting for all
if ~isnan(RANGE(1,1));lo = RANGE(1,1);end
if ~isnan(RANGE(1,2));hi = RANGE(1,2);end
else
if k<=size(RANGE,1) % individual ranges
if ~isnan(RANGE(k,1));lo = RANGE(k,1);end
if ~isnan(RANGE(k,2));hi = RANGE(k,2);end
end
end
end
subplot(N(1),N(2),k);
if lo==hi
imagesc(im(:,:,k));
else
imagesc(im(:,:,k),[lo hi]);
end
axis off
if exist('TITLE','var') && ~isempty(TITLE)
if k<=size(TITLE,1)
title(TITLE(k,:),'FontSize',10)
end
end
end
end

% if extra space, put focus on "next" plot
if k<=N(1)*N(2)
subplot(N(1),N(2),k)
axis off
end