0

I am trying to play the audio in synchronization with moving line on graph of audio after click on pushbutton in GUI matlab. the code for the above task is

  fs = 44100;
durT = 3; %seconds
durS = fs*durT; %samples
x = randn(durS, 1);

dt = 1/fs;
tAxis = dt:dt:durT;

frameRate = 25; %fps
frameT = 1/frameRate;

mag = 5;

figure;
plot(tAxis, x);
ylim([-mag mag])
xlim([0 durT])
xlabel('Time [s]')

playHeadLoc = 0;
hold on; ax = plot([playHeadLoc playHeadLoc], [-mag mag], 'r', 'LineWidth', 2);

player = audioplayer(x, fs);
myStruct.playHeadLoc = playHeadLoc;
myStruct.frameT = frameT;
myStruct.ax = ax;

set(player, 'UserData', myStruct);
set(player, 'TimerFcn', @apCallback);
set(player, 'TimerPeriod', frameT);
play(player);

the callback function is

function src = apCallback(src, eventdata)
    myStruct = get(src, 'UserData'); %//Unwrap

    newPlayHeadLoc = ...
        myStruct.playHeadLoc + ...
        myStruct.frameT;
    set(myStruct.ax, 'Xdata', [newPlayHeadLoc newPlayHeadLoc])

    myStruct.playHeadLoc = newPlayHeadLoc;
    set(src, 'UserData', myStruct); %//Rewrap
end

it is working well on matlab command window...but when i put this code in the callback function of pushbutton in GUI,it is just displaying the graph of signal with red line at the start.

i am not able to figure out why it is not working here.. please help. your contribution will appreciated greatly.

Autonomous
  • 8,935
  • 1
  • 38
  • 77
user3168654
  • 95
  • 1
  • 11

1 Answers1

2

Well just specify an axes to be used for Plotting and give it the initial default attributes, instead of using Figures.

 btn1_callbck (VAR)
 set(handles.axes1);
 plot(t,signal);
 end

OR

 btn1_callbck (VAR)
 figure(1000);
 plot(t,signal);
 end

hope this will help.

M.Khouli
  • 3,992
  • 1
  • 23
  • 26
  • Ok ... when you have GUI you should create an axes component to draw on (axes1).Then inside the callback function you should determine on which component you are going to draw (plot). btn1_callbck (VAR) set(handles.axes1); plot(t,signal); end OR btn1_callbck (VAR) figure(1000); plot(t,signal); end – M.Khouli May 14 '14 at 06:08