Trying to plot a function with adjustable variables in app designer

조회 수: 11 (최근 30일)
Rawsen
Rawsen 2023년 2월 28일
답변: Kevin Holly 2023년 4월 7일
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!

답변 (2개)

Cameron
Cameron 2023년 3월 1일
plot(app.UIAxes,x,y)

Kevin Holly
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

카테고리

Help CenterFile Exchange에서 Develop Apps Using App Designer에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by