Question cell structure, plotting and how to avoid a loop.
2 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
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
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
More Answers (1)
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
See Also
Categories
Find more on Geometric Transformation and Image Registration in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!