How to use inputdlg with plotting a bouncing ball?
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
I am trying to write a script that shows a ball bouncing around in a figure window. I have done it without using the inputdlg with this script...
clear; clc; close all;
figure(1); grid on; hold on; axis equal
axis([0 15 0 10])
xlast = 5; % initial location
ylast = 7.5; % initial location
ulast = -20; % u is our initial vertical velocity
delX = 0.01; % initial horizontal velocity
delT = 0.01; % time step
% system constants
g = -15.81;
% plot the current location of the ball
for ii = 2 : 10000;
unow = g*delT + ulast;
ynow = ulast*delT + ylast;
xnow = xlast + delX;
if ynow <= 0.0
unow = (-0.94)*unow;
ynow = 0.0;
end
if xnow >= 15
delX = (-0.94)*delX;
xnow = 15.0;
end
if xnow <= 0
delX = (-0.94)*delX;
xnow = 0.0;
end
delete(findobj('tag','delete me'));
plot(xnow, ynow, 'go', 'linewidth', 2, 'tag', 'delete me');
drawnow;
ulast = unow; xlast = xnow; ylast = ynow;
end
But I need to include the inputdlg function now and it keeps showing me an error in the plot command about vectors needing to be the same length. I think they are...
clear; clc; close all;
figure(1); grid on; hold on; axis equal
axis([0 12 0 12])
delT = 0.01; % time step
% system constants
g = -15.81;
prompt={'Ball1 initial X loc', 'Ball2 initial Y loc', 'Ball1 intial Y velocity', 'Ball1 initial X velocity'};
name='Initial Values';
numlines=1;
defaultanswer={'5','7.5','-20','0.01'};
output_values=inputdlg(prompt,name,numlines,defaultanswer);
for ii = 2 : 10000;
unow = g*delT + str2num(output_values{3});
ynow = (str2num(output_values{3}))*delT + str2num(output_values{2});
xnow = str2num(output_values{1}) + str2num(output_values{4});
if ynow <= 0.0
unow = (-0.94)*unow;
ynow = 0.0;
end
if xnow >= 12
delX = (-0.94)*str2num({4});
xnow = 12.0;
end
if xnow <= 0
delX = (-0.94)*str2num({4});
xnow = 0.0;
end
delete(findobj('tag','delete me'));
plot(xnow, ynow, 'go', 'linewidth', 2, 'tag', 'delete me');
drawnow;
str2num(output_values{3}) = unow; str2num(output_values{1}) = xnow; str2num(output_values{2}) = ynow;
end
Please help me figure out how to keep this same concept and include the use of the inputdlg box.
0 Commenti
Risposte (1)
Andrew Schenk
il 15 Giu 2015
There are some syntax errors in the second version of your code. For example,
delX = (-0.94)*str2num({4});
should instead be
delX = (-0.94)*str2num(output_values{4});
A better approach instead having to rewrite all of your MATLAB code using the output_values variable is to use the first version of your code and modify it so the variables are assigned from the inputdlg result as shown below:
prompt={'Ball1 initial X loc', 'Ball2 initial Y loc', 'Ball1 intial Y velocity', 'Ball1 initial X velocity'};
name='Initial Values';
numlines=1;
defaultanswer={'5','7.5','-20','0.01'};
output_values=inputdlg(prompt,name,numlines,defaultanswer);
xlast = str2num(output_values{1});
ylast = str2num(output_values{2});
ulast = str2num(output_values{3});
delX = str2num(output_values{4});
0 Commenti
Vedere anche
Categorie
Scopri di più su Entering Commands in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!