Question cell structure, plotting and how to avoid a loop.

2 views (last 30 days)
Hello everybody,
I have a 1x13 cell structure called "Im" containing 13 matrices of 512x512 dimension with integer number ( which represent images). I want to plot all 13 matrices along with a fit I made, which basically should be like this code:
plot(fit,Pixels,Im{:}(:,256))
As I am sure you know, I get an error called Bad cell reference operation. The code works if I choose just one cell, for example plot(fit,Pixels,Im{5}(:,256)) but doesn't work as I tried above. I would wish not to use a loop as I think it could be avoided.
Thank you very much in advance

Accepted Answer

Guillaume
Guillaume on 21 Sep 2014
Edited: Guillaume on 21 Sep 2014
Even if Im{:}(:, 256) was valid matlab syntax it still wouldn't work as plot expects a pair of x and y coordinate for each line, not just one x vector and a number of y vectors.
The way to extract column 256 from all the images is with:
cellfun(@(img), img(:, 256), Im, 'UniformOutput', false) %return a cell array of columns
plot also accepts a vector of x and matrix of y where each column or row is a line to plot, so I would just transform your image columns in a matrix and concatenate that with Pixels. Assuming that Pixels is a column vector:
plot(fit, [Pixels cell2mat(cellfun(@(img) img(:, 256), Im, 'uni', false))]);
edited for spelling and typos
  4 Comments
Guillaume
Guillaume on 21 Sep 2014
Sorry, there was an extra comma between @(img) and img(..) that shouldn't have been there (now corrected).
@(img) img(:, 256) is an anonymous function. It is equivalent to:
function out = myfun(img)
out = img):, 256);
end
The whole cellfun is equivalent to:
c = cell(size(Im));
for idx = 1:numel(Im)
img = Im{idx};
c{idx} = img(:, 256);
end
Filippos
Filippos on 21 Sep 2014
Many thanks, it has worked exactly as I wanted. Thank you very much and have a nice day sir.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 21 Sep 2014
I don't know what fit and Pixels are but presumably they're lists of x and y coordinates. Then, for the next argument of plot() you don't put a whole image. You display images with a separate call to imshow(), image(), or imagesc().
% First, loop to display all 13 images.
for k = 1 : length(Im)
thisImage = Im{k}; % Extract image
subplot(3, 5, k);
imshow(thisImage);
end
% Now plot fit vs. Pixels
subplot(3, 5, 15);
plot(fit, Pixels, 'b-', 'LineWidth', 3);
grid on;
xlabel('fit', 'FontSize', 20);
ylabel('Pixels', 'FontSize', 20);
title('Pixels vs. fit', 'FontSize', 20);
  2 Comments
Filippos
Filippos on 21 Sep 2014
I am sorry that I was not very clear. I do now want to produce the images, but the intensity profile in the middle of the image. The "fit" variable is the one you get from the cftool and the Pixels is just a vector 1:512. In the end I am planning to do something like this : http://tinypic.com/view.php?pic=2j0dnqr&s=8#.VB7KhFfitNg

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!