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 个评论
Das Siddharth
Das Siddharth 2021-4-19
I don't think so but I have seen it in one video but it only works for me when I don't. Btw the upper plot in which I want to plot the data the x and y axis keeps changing in response to that pop-up figure but the actual plotting is only limited to the figure.
That's the issue there are very few documentation about the new App designer and it's quite a mess.

请先登录,再进行评论。

采纳的回答

Cris LaPierre
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 个评论
Das Siddharth
Das Siddharth 2021-4-20
If I do so, there's no plot at all. On top of that it throws an error saying "it should be a handle."
Cris LaPierre
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
I use the startup function to set up the axes and obtain a startTime.
% 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 个)

类别

Help CenterFile Exchange 中查找有关 Animation 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by