MATLAB Answers

Seeing the image overlaid on image

1 view (last 30 days)
SS
SS on 4 Sep 2020
Answered: Madhav Thakker on 9 Sep 2020
I have this code with an image, and a diagonal line and a point plotted on the image. I want the point to move straight up and down whenever I press the space bar. When I run this code, I get two separate things - one with the image, line and dot, and one with the dot that moves up and down but on a blank background. What do I need to do to combine the two?
fs=100e3; % sampling frequency (Hz)
f=5000; % stimulus frequency (Hz)
dur = .0002; % duration (s)
ns = dur*fs; % # samples
t=(0:1:ns-1)/fs; % time vector (s)
a=1; % amplitude (arbitrary)xcsdg
phi=0; %phase (radians)
x=0; % x-position (fixed)
imshow('AvgBscan.tiff');
[rows, columns, numberOfColorChannels] = size('AvgBscan.tiff');
x = [0,150];
y = [0,100];
line(x,y, 'LineWidth', 4, 'Color', [1, 0, 1]);
plot(50,33,'yo', 'MarkerSize', 12)
for t_i = 1:length(t) % for each point in time
y = a*sin(2*pi*f*t(t_i) + phi); % calculate y-position of circle according to the output of a sine wave at that time
axis on
hold on;
h=figure;
plot([-1 1],[0 0],'k--');
xlim([-1 1]);
ylim([-1 1]);
pause; %close all;
end
  2 Comments
SS
SS on 4 Sep 2020
Hi, I did that and changed some things but still no luck. Would appreciate if you could assist further.
fs=100e3; % sampling frequency (Hz)
f=5000; % stimulus frequency (Hz)
dur = .0002; % duration (s)
ns = dur*fs; % # samples
t=(0:1:ns-1)/fs; % time vector (s)
a=1; % amplitude (arbitrary)
phi=0; %phase (radians)
x=50; % x-position (fixed)
h = plot(50,33,'yo', 'MarkerSize', 12)'
for t_i = 1:length(t) % for each point in time
imshow('AvgBscan.tiff');
axis on;
hold on;
y = a*sin(2*pi*f*t(t_i) + phi)
% plot circle at x vs. y
set(h, 'YData', Y);
drawnow
plot([-1 1],[0 0],'k--');
xlim([0 100]);
ylim([0 80]);
pause; %close all;
end

Sign in to comment.

Answers (1)

Madhav Thakker
Madhav Thakker on 9 Sep 2020
Hi,
You can use hold on for this purpose. You can change the pointer by changing the YData of the plot. I am attaching a code snippet for better understanding.
x = [-10,150];
y = [-10,150];
line(x,y, 'LineWidth', 4, 'Color', [1, 0, 1]);
hold on
pl = plot(0,0,'yo', 'MarkerSize', 12)
axis on
hold on
for t_i = 1:length(t) % for each point in time
y = a*sin(2*pi*f*t(t_i) + phi) % calculate y-position of circle according to the output of a sine wave at that time
hold on;
axis on;
pl.YData = y;
hold on
plot([-1 1],[0 0],'k--');
xlim([-1 1]);
ylim([-1 1]);
pause; %close all;
end
Hope this helps.

Community Treasure Hunt

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

Start Hunting!