Question cell structure, plotting and how to avoid a loop.
1 回表示 (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
Guillaume
2014 年 9 月 21 日
編集済み: Guillaume
2014 年 9 月 21 日
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 件のコメント
Guillaume
2014 年 9 月 21 日
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
その他の回答 (1 件)
Image Analyst
2014 年 9 月 21 日
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 件のコメント
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!