(App Designer) How can I plot multiple outputs on the same graph from the same function?

14 次查看(过去 30 天)
I'm trying to display different outputs of motion on the same graph, from the same function. I've seen examples of different equations being plotted on the same graph, but none on graphing outputs from the same function. The functions used take user input, plugs it in, and displays an output. I want for the graph to be able to hold multiple results so that the user can compare the results. How can the program recognize the new input data the plot a new graph? In addition, I'm trying to implement a check box so that the user can toggle holding the previous graphs. I am a beginner at MATLAB, so I apologize if my code is not as clean!
MATLAB code:
%
% REFERENCE FOR EQUATIONS AND USER INPUT
% initialvelocity = app.InitialVelocityEditField.Value;
% h = app.InitialHeightEditField.Value;
% a = app.AccelerationofGravityEditField.Value;
% initialvertveloc = initialvelocity * sind(theta);
% fy = @(t) h + initialvertveloc*t + (-0.5*a*(t.^2));
%
%graph for y motion
L = linspace(0,t,100);
Y2 = fy(L);
ymax2 = max(Y2)*1.5;
xmax2 = max(L)*1.5;
ax3 = app.UIAxes8;
scalemax2 = max(ymax2, xmax2);
ax3.XLim = [0 t];
ax3.YLim = [0 scalemax2];
plot(app.UIAxes8, L, Y2);
drawnow;
hold on
value1 = app.HoldPreviousDataCheckBox.Value;
while value1 = 1 %why is this not valid?
plot(app.UIAxes8, L, Y2);
drawnow;
hold on;
end

回答(1 个)

Cam Salzberger
Cam Salzberger 2018-8-3
It seems that you are doing the right thing to use hold, but for UI Figures (including App Designer), you need to pass the UI Axes into hold as the first argument, as noted here.
And while value1 = 1 is incorrect because "=" is the assignment operator and "==" is the comparison operator. So you want to use "==".
However, you probably mean to either extract the value of value1 within the loop, or just do: while app.HoldPreviousDataCheckBox.Value. Otherwise you have an infinite loop. And note that for a simple on/off value, you can just treat the value as the conditional. There's never a need to do: if x == true, you can just do: if x.
Hope that helps!
-Cam
  1 个评论
An Nguyen
An Nguyen 2018-8-3
Thank you for your advice! I understand the logic behind using the check box now, but the graph is still plotting only the newest outputs and not retaining previous outputs. Is there a way to hold all the new outputs? How would the program recognize the new inputs, run them, and plot them without re-plotting over the previous outputs?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Creating, Deleting, and Querying Graphics Objects 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by