App Designer 2021a - Plotting inside the GUI instead of a figure window
조회 수: 16 (최근 30일)
이전 댓글 표시
I am currently using the latest Matlab 2021a. In the app designer when I try to plot a contniously acquired data from a gas/temperature sensor using arduino, a pop up figure window opens up displaying the realtime data even when I specified the proper UIAxes.
I read that the "figure" is faster than the GUI Axes but stil it's kinda weird if you make an app and a separate pop up window opens instead of the Axes that you specifically put for the very purpose. I have also provided a portion of the code for a better understanding. Thank you !
% this code is for the pushbutton callback.
app.flag = 0;
global a;
app.h = animatedline;
ax = gca;
app.stop = false;
startTime = datetime('now');
while ~app.stop
v = readVoltage(a,'A0');
t = datetime('now') - startTime;
if v >= 0.5 && v <= 0.65
app.StatusLamp.Color = 'g';
else
app.StatusLamp.Color = [0.90,0.90,0.90];
end
%add point to animation
plot(app.UIAxes,datenum(t),v);
addpoints(app.h,datenum(t),v);
ax.XLim = datenum([t-seconds(15) t]);
datetick('x','keeplimits')
drawnow
app.stop = app.flag;
app.TemperatureCEditField.Value = v;
end
댓글 수: 4
Geoff Hayes
2021년 4월 19일
Hmm...when you create the animated line, should you specify the axes as per animated line axes input?
app.h = animatedline(app.UIAxes);
채택된 답변
Cris LaPierre
2021년 4월 19일
You need to specify the uiaxes to plot into in an app. Otherwise, your plot command will create a new figure window for the plot.
plot(app.UIAxes,...)
app.h = animatedline(app.UIAxes)
I'd also suggest using app.UIAxes instead of gca. It avoids any potential ambiguity
Another observation. You appear to be plotting your data one value at a time. When you use plot for a single data point, you must specify a marker style or it will not appear in the figure. Also, if you don't specify hold on, each plot command will clear the axes before plotting.
Also, it is recommended to avoid using datenum when possible. You should be able to directly plot using t, which should be a duration. Use xtickformat to set the display format.
댓글 수: 4
Cris LaPierre
2021년 4월 20일
편집: Cris LaPierre
2021년 4월 20일
I do need to make one correction. It looks like animatedlines do not accept anything other than doubles for X. That means modifying the approach slightly, but it's still doable.
We don't have enough of your code to actually run it, so I created a simplified example.
I created two properties to share data within the app.
properties (Access = private)
startTime % Start time
h % handle to animated line
end
% Code that executes after component creation
function startupFcn(app)
app.startTime = datetime('now');
plot(app.UIAxes,second(0),rand(1),'bs')
app.h = animatedline(app.UIAxes,second(0),rand(1));
end
I then added a new point every time a button was pushed. That code looked like this.
% Button pushed function: Button
function ButtonPushed(app, event)
t = datetime('now') - app.startTime;
hold(app.UIAxes,'on')
plot(app.UIAxes,seconds(t),rand(1),'bs')
hold(app.UIAxes,'off')
addpoints(app.h,seconds(t),rand(1))
end
The results of both plots appear in the UIAxes in the app.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/590217/image.png)
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Animation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!