MATLAB GUI: Axes do not retain modifications when using the plot function
1 view (last 30 days)
Show older comments
Hi, everyone I'm working on a Matlab GUI, I acquire samples over COM, there is a callback every N samples, then I plot them on GUI. The problem is as follow: in GUI Opening Function I make some axes initializations (axes tick, axes limits, axes labels...) and it's all correct when the GUI is opened but as soon as data are plotted the axes loose the lables and part of the YTick personalization. I've attached the code to discuss with you searching for my possible faults.
%Axes initialization
handles.axes1.YTickMode = 'manual';
handles.axes1.XTickMode = 'manual';
handles.axes1.YLimMode = 'manual';
handles.axes1.XLimMode = 'manual';
ytickformat(handles.axes1,'%.2f');
handles.axes1.XLabel.String = 'Samples';
handles.axes1.YLabel.String = 'Voltage';
handles.axes1.YLim = [0 3.0];
handles.axes1.XLim = [0 1000];
handles.axes1.YTick = [0 0.25 0.5 0.75 1.0 1.25 1.5 1.75 2.0 2.25 2.5 2.75 3.0];
handles.axes1.XTick = [0 250 500 750 1000];
%Serial Callback
function serialcallback(hObject)
handles = guidata(hObject);
buffer = fread(handles.serConn, 3000, 'uint16');
j=0;
for k = 1:3:3000
j = j+1;
handles.Volt(j) = double(buffer(k))*(3.0/4095.0);
if k > 1
handles.Temp(j-1) = buffer (k-2);
handles.Curr(j-1) = buffer (k-1);
end
end
plot(handles.axes1,handles.Volt);
guidata(hObject,handles);
Thank you to everyone for the support.
2 Comments
Jan
on 2 Jun 2018
Please post the relevant part of the code only. It is not efficient to let the readers search for the lines you are talking of. Thanks.
Accepted Answer
Jan
on 2 Jun 2018
Edited: Jan
on 2 Jun 2018
It is the documented behavior of the high-level graphics functions like plot to adjust the properties of the axes object automatically. To avoid this, you can use low-level functions like line, or fix the axes' properties by:
axes('NextPlot', 'add')
% Equivalent:
% hold('on');
I assume in your case:
handles.axes1.NextPlot = 'add';
This has the effect, that formerly existing line objects are not deleted by plot automatically anymore, such that you have to do this explicitly on demand:
delete(handles.axes1.Children)
By the way: Better omit the loop and make the code nicer in the serialcallback:
handles = guidata(hObject);
buffer = fread(handles.serConn, 3000, 'uint16');
handles.Volt = double(buffer(1:3:end) * (3.0/4095.0);
handles.Temp = buffer(2:3:end);
handles.Curr = buffer(3:3:end);
2 Comments
Jan
on 2 Jun 2018
The "vectorized" approach without the loop is faster and offers less chances for a typo during programming.
More Answers (0)
See Also
Categories
Find more on Graphics Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!