How to do simultaneous flickering of two straight line plots

2 Ansichten (letzte 30 Tage)
Marisa
Marisa am 30 Mär. 2022
Kommentiert: Marisa am 1 Apr. 2022
I have plotted a number 7 using two straight line plots.I want these lines to flicker for 5 seconds at different frequency but start simultaneously.I have done flickering at different frequency but in a sequential maaner.How can I achieve this flickering simultaneously?
This is my code
a=plot([6;9],[0.4;0.4],'linewidth',8,'color','green')
hold on
% t=text(0.5,0.5,'\leftarrow');
set(gca,'color',[0 0 0])
xlim([0 12])
ylim([-0.4 0.8])
b=plot([9;9],[-0.1 0.4],'linewidth',8,'color','green')
xlim([0 12])
ylim([-0.4 0.8])
t0=clock
while etime(clock,t0) <5
screensize = get( groot, 'Screensize' );
%for blinks=1:10
set(a,'visible','off')
pause(2)
set(a,'visible','on')
pause(2)
set(b,'visible','off')
pause(3)
set(b,'visible','on')
pause(3)
end
  1 Kommentar
Marisa
Marisa am 1 Apr. 2022
Thank you .I got the flickering result but wanted it in a parallel manner so I used parfor instead of for and it gives the result directly without flickering.Please give me solution to this

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Simon Chan
Simon Chan am 31 Mär. 2022
Use function timer to set one timer for plot a and the other for plot b.
Attahced please find the function and you can run it to see whether this is what you want or not.
You may adjust the Period and TasksToExecute for different frequency and how many times you want to repeat,
u = 0; v=0;
t1 = timer('StartDelay', 1, 'Period', 0.25, 'TasksToExecute', 20, 'ExecutionMode', 'fixedRate');
t1.TimerFcn = @flashu;
t2 = timer('StartDelay', 1, 'Period', 0.25, 'TasksToExecute', 20, 'ExecutionMode', 'fixedRate');
t2.TimerFcn = @flashv;
function flashu(src,event)
if mod(u,2)==0
set(a,'visible','off')
else
set(a,'Visible','on');
end
u = u+1;
end
function flashv(src,event)
if mod(v,2)==0
set(b,'visible','off')
else
set(b,'Visible','on');
end
v = v+1;
end

Weitere Antworten (1)

Mathieu NOE
Mathieu NOE am 30 Mär. 2022
hello Marissa
try this code
all the best
clc
clearvars
clf
figure(1)
a=plot([6;9],[0.4;0.4],'linewidth',8,'color','green');
hold on
% t=text(0.5,0.5,'\leftarrow');
set(gca,'color',[0 0 0])
xlim([0 12])
ylim([-0.4 0.8])
b=plot([9;9],[-0.1 0.4],'linewidth',8,'color','green');
xlim([0 12])
ylim([-0.4 0.8])
screensize = get( groot, 'Screensize' );
hold off
dt = 0.1;
t = (0:dt:5);
f1 = 1; % freq update for "a" plot (here 1 s off and 1 s on)
f2 = 2; % freq update for "b" plot (here 0.5 s off and 0.5 s on)
x1 = sin(pi*f1*t);
x2 = sin(pi*f2*t);
tic
for ci =1:length(t)
pause(dt)
if x1(ci)>=0
set(a,'visible','off')
else
set(a,'visible','on')
end
if x2(ci)>=0
set(b,'visible','off')
else
set(b,'visible','on')
end
end
toc

Community Treasure Hunt

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

Start Hunting!

Translated by