Trying to plot a function with adjustable variables in app designer
11 次查看(过去 30 天)
显示 更早的评论
I am trying to teach myself matlab, but I am running into a few problems:
1.) I am trying to plot the trajectory of a ball at a certain angle and velocity at the press of a button. Here is what I have so far.
and my code for the button looks like this:
%variables
x0 = 0;
y0 = 0;
time = 50;
a = 45;
angle = a*(pi./180);
v = 70;
g = 9.81;
t = 0:.1:time;
%functions
x = x0+v*cos(angle)*t;
y = y0+v*sin(angle)*t-(g*t.^2)/2;
plot(x,y)
I don't know why, but when I run the program and press the button, the tick marks on the axis don't show and nothing is plotted on the graph.
2.) I have a function.m file that successfully graphs what I need it to. Is there a way for me to reference that in the app?
3.) Eventually, I would like to make the velocity and angle variables adjustable through the edit field and slider. How would I do this?
like I said, I'm very new to matlab and I am trying to teach myself. If my questions are unclear, feel free to reach out to me so I can try to clarify it. Thank you!
0 个评论
回答(2 个)
Kevin Holly
2023-4-7
2.) I have a function .m file that successfully graphs what I need it to. Is there a way for me to reference that in the app?
Yes, you can call it just like in any script by writing the function's name. Make sure that functionname.m is in your path. In this case, you may want to make sure 'app' is included as an input to the function. Then you can call it as:
functionname(app)
3.) Eventually, I would like to make the velocity and angle variables adjustable through the edit field and slider. How would I do this?
Right click the "Run" button in design view and add a callback.
% Button pushed function: RunButton
function RunButtonPushed(app, event)
functionname(app) % This line references a function either within app or in a .m or .mlx file.
end
The function below pulls data from the uicomponents (uislider and uieditfield) and plot it on the uiaxes.
function functionname(app)
x0 = 0;
y0 = 0;
a = app.AngleSlider.Value;
angle = a*(pi./180);
v = app.VelocityEditField.Value % make sure you are using a numeric slider
g = 9.81;
t = 0:.1:time;
x = x0+v*cos(angle)*t;
y = y0+v*sin(angle)*t-(g*t.^2)/2;
plot(app.UIAxes,x,y)
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Develop Apps Using App Designer 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!