How can I change variables within a GUI to make plot using the data from user input?

1 回表示 (過去 30 日間)
Mike Stevens
Mike Stevens 2019 年 4 月 18 日
編集済み: Adam 2019 年 4 月 18 日
I am making a GUI for displaying the path of a projectile, moving with projectile motion. How this will work is that you select wether you are on the Earth or Moon for gravitational acceleration via uicontrol's popup menu. Both the launch velocity and angle will be selected using the same method.
After getting the figure window and the popup menus for the gravity and velocity conditions to display correctly, I tried to use callback functions to set variables which will be used in formulae later in the script.
So how would you change variables once data has been selected using uicontrol components?
I am pretty new to this and have not really found a working solution so far. If you do provide a solution, could you explain it with as much depth as possible to help my understanding of this?
Below is the function for the GUI:
function ballistic
clc
f = figure('Visible','off','Position',[360,500,600,285]);
contxt=uicontrol('Style','text','String','Conditions:',...
'Position',[462,260,60,15]);
veltxt=uicontrol('Style','text','String','Launch Velocity:',...
'Position',[457,200,100,15]);
mps1=uicontrol('Style','text','String','m/s',...
'Position',[548,180,60,15]);
con=uicontrol('Style','popupmenu',...
'String',{'Earth','Moon'},'Position',[465,240,100,20],...
'Callback',@callback_con);
vel=uicontrol('Style','popupmenu',...
'String',[1:1:100],'Position',[465,180,100,20],...
'Callback',@callback_vel);
function callback_con(source,eventdata)
str=source.String;
val=source.Value;
switch str{val};
case 'Earth'
g=9.81;
case 'Moon'
g=1.62;
end
end
function callback_vel(source,eventdata)
switch source.Value
case source.Value>0
velzero=source.Value;
end
end
f.Units = 'normalized';
contxt.Units = 'normalized';
veltxt.Units = 'normalized';
mpsl.Units = 'normalized';
con.Units = 'normalized';
vel.Units = 'normalized';
f.Name = 'Ballistic Path Visualisation';
movegui(f,'center')
f.Visible = 'on';
end

回答 (1 件)

Adam
Adam 2019 年 4 月 18 日
編集済み: Adam 2019 年 4 月 18 日
Simplest option is simply to create variables
g = [];
valzero = [];
(or initialise them to a default value)
at the top of your function.
Because you are using nested functions these variables will then be shared with these nested functions and so when the nested functions make changes the variables in the main function will also change.
As an aside, you should never have a function with 'clc' in it. You want functions to do what they are expected to do by their name and (if any) input arguments. They shouldn't have side-effects like casually clearing your command window.
Just run it as:
clc; ballistic
if you want to do this.

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

タグ

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by