我试图在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的按钮回调函数中,它只是在开始时显示带有红线的信号图形。
我不明白为什么这里不工作。请帮助。我们只需指定用于绘图的轴,并赋予其初始默认属性,而不是使用Figures。
btn1_callbck (VAR)
set(handles.axes1);
plot(t,signal);
end
或
btn1_callbck (VAR)
figure(1000);
plot(t,signal);
end