Plot a column from an array by clicking on an image of that array

2 views (last 30 days)
Say I have an array an image of which I can display:
ti = magic(32);
figure(1);
imagesc(ti);
I want to be able to click on a pixel in the image frame and have a plot updated to show the column from the original array that includes that pixel.
while 1
figure(1);
[x,y] = ginput;
figure(2);
plot(ti(:,round(x)));
end
Except that this doesn't work.
What I really want is a tool in an image figure which plots the columnar profile if I click the tool on the image.
Thanks.

Accepted Answer

Image Analyst
Image Analyst on 13 Jan 2023
Try this:
ti = magic(32);
subplot(2, 1, 1);
imshow(ti, [], 'InitialMagnification', 1000)
g = gcf;
g.WindowState = 'maximized';
buttonText = 'OK';
while strcmpi(buttonText, 'OK')
subplot(2, 1, 1);
promptMessage = sprintf('Click a point');
titleBarCaption = 'Continue?';
buttonText = questdlg(promptMessage, titleBarCaption, 'OK', 'Quit', 'OK');
if contains(buttonText, 'Quit', 'IgnoreCase', true)
return; % or break or continue.
end
[x, y] = ginput(1);
column = round(x);
thisColumn = ti(:, column);
subplot(2, 1, 2);
plot(thisColumn, 'b-', 'LineWidth', 2);
grid on;
caption = sprintf('Column %d', column);
title(caption, 'FontSize', 16)
xlabel('Row', 'FontSize', 16)
ylabel('Value', 'FontSize', 16)
end

More Answers (0)

Categories

Find more on Visual Exploration in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!