在matlab GUI中单击按钮时播放声音

在matlab GUI中单击按钮时播放声音,matlab,user-interface,qpushbutton,Matlab,User Interface,Qpushbutton,我试图在点击GUI matlab中的按钮后,与音频图形上的移动线同步播放音频。上述任务的代码是 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 ma

我试图在点击GUI matlab中的按钮后,与音频图形上的移动线同步播放音频。上述任务的代码是

  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);
回调函数是

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
它在matlab命令窗口中运行良好…但当我将此代码放入GUI中按钮的回调函数中时,它只是在开始时显示带有红线的信号图

我不明白为什么它在这里不起作用。。
请帮忙。非常感谢您的贡献。

我们只需指定一个用于打印的轴,并为其提供初始默认属性,而不用图形

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


希望这会有所帮助。

好的。。。当您有GUI时,您应该创建一个要绘制的轴组件(axes1)。然后在回调函数中,您应该确定要在哪个组件上绘制(绘图)。btn1_callbck(VAR)集合(handles.axes1);绘图(t,信号);结束或btn1_callbck(VAR)图(1000);绘图(t,信号);结束
 btn1_callbck (VAR)
 figure(1000);
 plot(t,signal);
 end