App Designer 2021a - Plotting inside the GUI instead of a figure window
30 次查看(过去 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.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!