MATLAB: MATLAB GUI Updating plot

guiplotplot update

I have a MATLAB gui that demonstrates the plotting of real-time incoming data signal and a horizontal threshold line:
axes(handles.axes1);
plot([1 windowLength].*1/sampleFreq, [-data1Threshold -data1Threshold],'k','linewidth',2);
% Plotting the threshold line
xlim([1 windowLength].*1/sampleFreq);
ylim([-300 300]);
xlabel('Time (s)')
ylabel('Filtered signal (uV)')
hold on;
plot([1:10:length(data1)].*1/sampleFreq, data1(1:10:end),'b','linewidth',2);
% Plotting the signal over the threshold line
hold off;
drawnow;
This part of the code is inside a while loop so that different parts of the signal (incoming signal) is plotted while the threshold line is identical. The issue is that the gui runs really slow. Is there anyway I can fix the threshold line and the axis information, so that I can only update the incoming signal in order to improve the speed?
I have tried the following to fix the handles to the plot, however, this still requires me to plot the threshold line and the signal at every iteration… (also not sure how to use set function with multiple data lines to plot, the threshold line and signal)
handles.plot1 = plot([1 windowLength].*1/sampleFreq, [-data1Threshold -data1Threshold],'k',[1:1:length(data1)].*1/sampleFreq, data1,'b','linewidth',2);
set(handles.plot1,'xdata',[1:10:length(data1)].*1/sampleFreq, 'ydata',data1(1:10:end));
any help will be greatly appreciated

Best Answer

thisax = handles.axes1;
plot2handle = get(thisax, 'UserData');
if isempty(plot2handle) || ~isgraphics(plot2handle) || ~strcmp(get(plot2handle,'type'),'line')
plot([1 windowLength].*1/sampleFreq, [-data1Threshold -data1Threshold],'k','linewidth',2, 'Parent', thisax);
% Plotting the threshold line
xlim(thisax, [1 windowLength].*1/sampleFreq);
ylim(thisax, [-300 300]);
xlabel(thisax, 'Time (s)')
ylabel(thisax, 'Filtered signal (uV)')
hold(thisax, 'on');
plot2handle = plot([1:10:length(data1)].*1/sampleFreq, data1(1:10:end),'b','linewidth',2, 'Parent', thisax);
% Plotting the signal over the threshold line
hold(thisax, 'off');
set(thisax, 'UserData', plot2handle);
else
%if it was a valid line handle, just update it
set(plot2handle, 'XData', [1:10:length(data1)].*1/sampleFreq, 'YData', data1(1:10:end));
end
Related Question