How do you plot an updating figure in a GUI?

2 views (last 30 days)
Hey, I am working on a golf GUI that displays the golf ball's path through a projectile motion drag equation. I want the projectile plot to update and look like real time (like the ball is moving). I have done this many times before in a for loop only not in a GUI and every time I try to do it in the GUI it is either very, very, very slow or it will not work. Does anyone know how to fix this? Below is my part of my push button function for the GUI.
Thanks!
% % % function edit5_Callback(hObject, eventdata, handles) % % hObject handle to edit5 (see GCBO) % % eventdata reserved - to be defined in a future version of MATLAB % % handles structure with handles and user data (see GUIDATA) % % % Hints: get(hObject,'String') returns contents of edit5 as text % % str2double(get(hObject,'String')) returns contents of edit5 as a double % % % % --- Executes during object creation, after setting all properties. % function edit5_CreateFcn(hObject, eventdata, handles) % % hObject handle to edit5 (see GCBO) % % eventdata reserved - to be defined in a future version of MATLAB % % handles empty - handles not created until after all CreateFcns called % % % Hint: edit controls usually have a white background on Windows. % % See ISPC and COMPUTER. % if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) % set(hObject,'BackgroundColor','white'); % end %
% --- Executes on button press in SwingButton. function SwingButton_Callback(hObject, eventdata, handles) % hObject handle to SwingButton (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % % When the push button is executed, calculations from all of the other % functions will be used to calculate other variables as well as setting % the axes and plotting the projectile. % v = str2double(get(handles.VelocityBox,'String')); c = get(handles.ballpopup,'UserData'); theta = get(handles.clubpopup,'UserData'); rho_grav = get(handles.planetpopup,'UserData'); rho = rho_grav(1); % Air density g = rho_grav(2); hole = get(handles.holepopup,'UserData'); hole_x = hole(1); hole_y = hole(2); a = pi*(.0366*2)^2/4; % Area of the ball D = rho*c*a/2; % Drag on the ball vx = v*cosd(theta); % Velocity in the x direction vy = v*sind(theta); % Velocity in the y direction dt = 0.001; % Change in time t(1) = 0; x(1) = 0; y(1) = 0; k = 1; m = 0.18; while min(y)> -dt % The loop will execute for y values greater than zero ax = -(D/m)*v*vx; ay = -g-(D/m)*v*vy; vx = vx+ax*dt; vy = vy+ay*dt; vx1(k+1) = vx+ax*dt; vy1(k+1) = vy+ay*dt; v = sqrt(vx^2 + vy^2); x(k+1) = x(k)+vx*dt+.5*ax*dt^2; y(k+1) = y(k)+vy*dt+.5*ay*dt^2; t(k+1) = t(k)+dt; k = k+1; end % % Projectile Plot (axes1) % x = x*1.09361; % meters to yards y = y*1.09361; % meters to yards %axes(handles.axes1); %hold on axes(handles.axes1); for i = 1:length(x) %plot(x,y,'k--',hole_x, hole_y,'ro') plot(x(i),y(i),'r-') %axis([0 380 0 250]) end title('\bfProjectile Path') xlabel('\bfX Distance (yards)') ylabel('\bfY Distance (yards)') axis([0 380 0 250])

Answers (0)

Categories

Find more on Numerical Integration and Differential Equations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!