How to hold previous figures when using imagesc?

18 次查看(过去 30 天)
I have many datasets (two of them provided below) to be plotted on a 256x256 pixels image. The first column in each dataset is the pixel index and the second column -intensity. When I am plotting this using 'imagesc', the figure does not update/hold rather only the last dataset gets plotted. Can anyone suggest a code or modify my code below to have all the datasets plotted on a single plot? Many thanks in advance.
Set 1:
26319 2
25806 6
26322 2
25811 7
25295 2
25298 4
26320 8
26321 8
25551 19
25554 18
26063 19
26066 18
25296 9
25297 9
26064 60
26065 61
25552 59
25553 62
25807 49
25810 58
25808 390
25809 378
set 2:
47291 31
47290 7
46269 37
46012 10
47033 34
46010 22
47037 39
47036 129
46011 40
46265 42
46525 94
46268 133
46777 95
46781 95
46267 165
46266 129
46523 238
46522 194
46779 228
46778 195
47035 172
47034 136
46524 194
46521 96
46780 212
The code:
for loop to access each dataset..
H11 = zeros(256,256);
H11(column 1) = column 2;
figure(1)
ax = gca;
imagesc(H11); f1=colorbar; ylabel(f1, 'TOT'); hold(ax, 'on');
colormap(ax, 'jet');
colorbar(ax);
hold(ax, 'on');
xlabel(ax, 'Horizontal pixels');
ylabel(ax, 'Vertical pixels');
end

回答(2 个)

Saffan
Saffan 2023-5-29
Hi Raju,
imagesc command creates a new plot every time it is called and hence image is overwritten by the last dataset and hold does not work as expected. You need to create a running sum of all the datasets in each iteration and then plot the sum after the loop.
Here is an example code snippet:
H_sum = zeros(256,256);
for loop to access each dataset..
H11 = zeros(256,256);
H11(column 1) = column 2;
% creating a running sum
H_sum = H_sum + H11;
end
figure(1)
ax = gca;
imagesc(H_sum);
f1=colorbar;
ylabel(f1, 'TOT'); hold(ax, 'on');
colormap(ax, 'jet');
colorbar(ax);
hold(ax, 'on');
xlabel(ax, 'Horizontal pixels');
ylabel(ax, 'Vertical pixels');

Image Analyst
Image Analyst 2023-5-29
编辑:Image Analyst 2023-5-29
If you want to paste an opaque copy of the newest image on top of existing images, see my attached copy and paste demo.
If you want plots inset to an image, see attached demos.

类别

Help CenterFile Exchange 中查找有关 Convert Image Type 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by