Push Button on a GUI problems
7 次查看(过去 30 天)
显示 更早的评论
Patrick
2011-4-23
I am making a GUI with 3 push buttons. Everything works fine, push buttons included. But after I press the push buttons a total of 8 times (it does not matter in which order I press them), they stop working. Can someone help me? I would really appreciate it.
Thanks
4 个评论
Paulo Silva
2011-4-23
I will check in my crystal ball, meanwhile post the code or share the files because my crystal ball might fail to get you an answer.
Matt Fig
2011-4-24
750 lines as pasted. I would like to help, but that is just way too much to wade through. I suggest you narrow it down as much as you can, then re-post...
回答(2 个)
Patrick
2011-4-23
Wow, that came out bad, but I'll just repost the part of the code with the push buttons and the functions that they call.
Push Button 1:
function startButton_Callback(hObject, eventdata, handles) % hObject handle to startButton (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) start=get(handles.playerMenu,'Value'); %Gets player information venue=get(handles.venueMenu,'Value'); %Gets venue information
if start==1 && venue==1
msgbox('Please select a player and venue!')
elseif start==1
msgbox('Please select a player!')
elseif venue==1
msgbox('Please select a venue!')
else
iVenue=get(handles.venueMenu,'Value');
switch iVenue
case 2
axes(handles.gamesAxes);
img=imread('joycebckgrnd.jpeg');
imagesc([0 27], [-2 17],img);
set(gca,'YDir','normal')
set(handles.gamesAxes,'XTick',[])
set(handles.gamesAxes,'YTick',[])
case 3
axes(handles.gamesAxes);
img=imread('ruckerbckgrnd.jpeg');
imagesc([0 27], [-2 17],img);
set(gca,'YDir','normal')
set(handles.gamesAxes,'XTick',[])
set(handles.gamesAxes,'YTick',[])
end
hold on
x=randi(21); %Generates random location for ball
a=get(handles.feetText,'String');
anum=str2double(a);
b=get(handles.inchesText,'String');
bn=str2double(b);
bnum=bn/12;
y=anum+bnum; %Sets ball at height of player
hold on
%ball_diameter = 1.4;
%ball=imread('ball.jpg');
%imagesc([x x+ball_diameter], [y y+ball_diameter],ball);
%set(gca,'YDir','normal'); %Sets image of ball at location
%%Plot and mask ball
ballimage=imread('ball.jpg');
ballmask = ballimage(:,:,3) > 150;
img=image('CData',ballimage);
ball_diameter = 1.4;
set(img,'XData',[x x+ball_diameter]);
set(img,'YData',[y y+ball_diameter]);
set(img,'AlphaData',~ballmask);
%%Draw Basket
% Draw net;
netx=[22.25 23.15 23.65 24.15 25 24.6 24.25 23.65 23 22.65 22.25];
nety=[12 9.75 12 9.75 12 9.75 12 9.75 12 9.75 12];
line(netx,nety, 'Color','w')
% Draw rim
rimx=[22.25 25.5];
rimy=[12 12];
line(rimx, rimy, 'Color', 'r', 'LineWidth', 4)
% Draw board
boardx=[25.5 25.5];
boardy= [11 16.75];
line(boardx,boardy, 'Color', 'w', 'LineWidth', 8)
% Draw support
supportx=[25.75 27];
supporty=[15 17];
line(supportx, supporty, 'Color', 'w', 'LineWidth', 6)
%%Initial Distance/Score
set(handles.distanceValue,'String', num2str(21-(x))); %Displays distance
set(handles.scoreValue,'String', num2str(0)); %Sets score to zero
set(handles.timeText,'String',100);
%t=str2double(get(handles.timeText,'String'));
%c=timer;
%set(c, 'executionMode', 'FixedRate')
%set(c, 'TimerFcn','t=t-1')
%start(c)
%set(handles.timeText, 'String', num2str(t))
%t=linspace(1,100);
%for it=1:101 %Starts timer
% set(handles.timeText,'String', num2str(101-t(it)));
% c=get(handles.timeText,'String');
% cnum=str2double(c);
%if cnum <=15
% set(handles.timeText,'ForegroundColor',[1 0 0]);
%end
%pause(1)
%end
powerBar(handles);
hold off
end
Push Button 2:
function shootButton_Callback(hObject, eventdata, handles) % hObject handle to shootButton (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
x = get(handles.angleValue, 'String');
if x=='--'
msgbox('Please press start before you shoot!')
else
ball(handles);
end
Push Button 3:
x=get(handles.powerValue,'String');
venue=get(handles.venueMenu,'Value');
if x=='--'
msgbox('Please press start before you shoot!')
else
angleBar(handles);
end
ball.m
function ball(handles)
%% Create background
iVenue=get(handles.venueMenu,'Value');
switch iVenue
case 2
axes(handles.gamesAxes);
img=imread('joycebckgrnd.jpeg');
imagesc([0 27], [-2 17],img);
set(gca,'YDir','normal');
axis equal
case 3
axes(handles.gamesAxes);
img=imread('ruckerbckgrnd.jpeg');
imagesc([0 27], [-2 17],img);
set(gca,'YDir','normal');
axis equal
end
hold on
%% Plot ball
%% set parameters
g=32.174; % ft/s^2 acceleration due to gravity
theta = str2double(get(handles.angleValue,'String'));
ax=0;
ay=-g ;
x=str2double(get(handles.distanceValue,'String'));
x0=21-x; %m
a=get(handles.feetText,'String');
anum=str2double(a);
b=get(handles.inchesText,'String');
bn=str2double(b);
bnum=bn/12;
y0=anum+bnum;
power=str2double(get(handles.powerValue,'String'));
velo = 40*(power/100);
vx0 =velo*cosd(theta); %ft/ s
vy0=velo*sind(theta);
Tf =45; %s
Nt=800;
ballimage = imread('ball.jpg');
ballmask = ballimage(:,:,3) > 150;
img=image('CData',ballimage);
ball_diameter = 1.4; % feet
%% initialize
t =linspace(0 , Tf, Nt) ;
x=zeros(1 , Nt);
y=zeros(1 , Nt);
vx=zeros(1 , Nt);
vy=zeros(1 , Nt);
deltat = t(2)-t(1);
x(1)= x0 ;
y(1)= y0;
vx(1)=vx0;
vy(1)=vy0;
for it = 1: Nt-1
vx(it+1)=vx(it)+ax*deltat;
vy(it+1)=vy(it)+ay*deltat;
x(it+1)=x(it)+ 0.5*(vx(it+1)+vx(it))*deltat;
y(it+1)=y(it)+ 0.5*(vy(it+1)+vy(it))*deltat;
%%Ground bounce condition
if (y(it+1)<0)
vy(it+1) = (abs((sin(pi/4))*vy(it+1)));
if abs(vy(it+1)) < 0.5
vy(it+1) = 0;
y(it+1) = 0;
end
%%Rolling condition
if vy(it+1) == 0;
if vx(it+1) < 0
ax = 0.1*g;
if abs(vx(it+1))<0.2
vx(it+1) = 0;
end
elseif vx(it+1) > 0
ax = -0.1*g;
if abs(vx(it+1))<0.2
vx(it+1) = 0;
end
end
end
end
%%Rim bouncing conditions
if (x(it+1)>=20.85) && (x(it+1)<=22.25) && (y(it+1)>=10.6) &&
(y(it+1)<=12)
if vy(it+1) >= 0
vy(it+1) = -(abs((sin(pi/4))*vy(it+1)));
y(it+1) = 10.59;
if x(it+1)<21.55
vx(it+1) = -abs((sin(pi/4))*vx(it+1));
elseif x(it+1)>=21.55
vx(it+1) = abs((sin(pi/4))*vx(it+1));
end
elseif vy(it+1) <= 0
vy(it+1) = (abs((sin(pi/4))*vy(it+1)));
y(it+1) = 12.001;
if x(it+1)<21.55
vx(it+1) = -abs((sin(pi/4))*vx(it+1));
elseif x(it+1)>=21.55
vx(it+1) = abs((sin(pi/4))*vx(it+1));
end
end
end
%%Back Rim
if (x(it+1)>=23.9) && (x(it+1)<=24.4) && (y(it+1)>=10.6) &&
(y(it+1)<=12)
if vy(it+1) >= 0
vy(it+1) = -(abs((sin(pi/4))*vy(it+1)));
y(it+1) = 10.59;
if x(it+1)<24.15
vx(it+1) = -abs((sin(pi/4))*vx(it+1));
elseif x(it+1)>=24.15
vx(it+1) = abs((sin(pi/4))*vx(it+1));
end
elseif vy(it+1) <= 0
vy(it+1) = (abs((sin(pi/4))*vy(it+1)));
y(it+1) = 12.001;
if x(it+1)<24.15
vx(it+1) = -abs((sin(pi/4))*vx(it+1));
elseif x(it+1)>=24.15
vx(it+1) = abs((sin(pi/4))*vx(it+1));
end
end
end
%%Backboard Bouncing Conditions
if (x(it+1)<25.8) && (x(it+1)>24.4) && (y(it+1)>=9.6) &&
(y(it+1)<=15)
if vx(it+1)>=0
vx(it+1) = -abs((sin(pi/4))*vx(it+1));
x(it+1) = 24.125;
elseif vx(it+1)<=0
vx(it+1) = abs((sin(pi/4))*vx(it+1));
x(it+1) = 25.5;
end
end
%%Scoring Conditions
if (x(it+1)>22.25) && (x(it+1)<23.9) && (y(it+1)>=10.6) &&
(y(it+1)<=11.6) && vy(it+1)<0
y(it+1) = 10.6;
end
%%Wall bouncing conditions
if (x(it+1)>25.6)
vx(it+1) = -abs((sin(pi/4))*vx(it+1));
elseif (x(it+1)<0)
vx(it+1) = abs((sin(pi/4))*vx(it+1));
end
end
%% plot results animating motion
xmax=27;
xmin=0;
ymax=17;
ymin=-2;
% for it =1:5:Nt
%plot(x(it) , y(it), 'ro');
%axis ([ xmin xmax ymin ymax ] ) ;
%xlabel('x(m)');
%ylabel('y(m)');
%drawnow
%end
%set(ball_image, 'Parent', hgtransform);
%% Draw net
netx=[22.25 23.15 23.65 24.15 25 24.6 24.25 23.65 23 22.65 22.25];
nety=[12 9.75 12 9.75 12 9.75 12 9.75 12 9.75 12];
line(netx,nety, 'Color', 'w')
%% Draw rim
rimx=[22.25 25.8];
rimy=[12 12];
line(rimx, rimy, 'Color', 'r', 'LineWidth', 4)
%% Draw board
boardx=[25.8 25.8];
boardy= [11 16.75];
line(boardx,boardy, 'Color', 'w', 'LineWidth', 8)
%% Draw support
supportx=[25.75 27];
supporty=[15 17];
line(supportx, supporty, 'Color', 'w', 'LineWidth', 6)
for it = 1:125;
%%Scoring Conditions
if (x(it+1)>22.25) && (x(it+1)<23.9) && (y(it+1)>=10.6) &&
(y(it+1)<=11.6) && vy(it+1)<0
score=str2double(get(handles.scoreValue,'String'));
distance=str2double(get(handles.distanceValue,'String'));
if distance<20
set(handles.scoreValue,'String',num2str(score+2));
elseif distance >=20
set(handles.scoreValue,'String',num2str(score+3));
end
end
%%Plot of the ball (spin doesn't work yet)
%angle_spin = 30;
%ball_spin = imrotate(ball_image, angle_spin);
%rot_angle = -(it/Nt)*pi;
%rball = makehgtform('zrotate', rot_angle);
set(img,'XData',[x(it) x(it)+ball_diameter]);
set(img,'YData',[y(it) y(it)+ball_diameter]);
set(img,'AlphaData',~ballmask);
axis([xmin xmax ymin ymax]);
drawnow
end
%% spawn position of new ball
axes(handles.gamesAxes)
pos_x=randi(21);
a=get(handles.feetText,'String');
anum=str2double(a);
b=get(handles.inchesText,'String');
bn=str2double(b);
bnum=bn/12;
pos_y=anum+bnum;
set(handles.distanceValue,'String', num2str(21-(pos_x)));
%% Plot and mask ball
ball_diameter = 1.4;
ballimage=imread('ball.jpg');
ballmask = ballimage(:,:,3) > 150;
img=image('CData',ballimage);
ball_diameter = 1.4;
set(img,'XData',[pos_x pos_x+ball_diameter]);
set(img,'YData',[pos_y pos_y+ball_diameter]);
set(img,'AlphaData',~ballmask);
%ball=imread('ball.jpg');
%imagesc([pos_x pos_x+ball_diameter], [pos_y pos_y+ball_diameter],ball);
powerBar(handles)
---------------
angleBar.m
function angleBar(handles)
x=1;
a=linspace(1,90,90);
Nt=length(a);
ix=100;
while ix>0
for it=1:Nt
bar(handles.angleAxes,1,a(it));
axis(handles.angleAxes,[0 2 0 90])
set(handles.angleAxes,'XColor',[0.749 0.749 0])
set(handles.angleAxes,'YColor',[0.749 0.749 0])
set(handles.angleAxes,'XTick',[])
set(handles.gamesAxes,'XTick',[])
set(handles.gamesAxes,'YTick',[])
drawnow
handles.angle=a(it);
u=num2str(handles.angle);
set(handles.angleValue,'String',u)
end
for it=1:Nt
ia=90-a(it);
bar(handles.angleAxes,1,ia);
axis(handles.angleAxes,[0 2 0 90])
set(handles.angleAxes,'XColor',[0.749 0.749 0])
set(handles.angleAxes,'YColor',[0.749 0.749 0])
set(handles.angleAxes,'XTick',[])
set(handles.gamesAxes,'XTick',[])
set(handles.gamesAxes,'YTick',[])
drawnow
handles.angle=ia;
u=num2str(handles.angle);
set(handles.angleValue,'String',u)
end
ix=ix-1;
end
----------------------- powerBar.m
function powerBar(handles)
x=1;
y=linspace(1,100);
Nt=length(y);
ix=100;
while ix>0
for it=1:Nt
bar(handles.powerAxes,1,y(it));
axis(handles.powerAxes,[0 2 0 100])
set(handles.powerAxes,'XColor',[0.749 0.749 0])
set(handles.powerAxes,'YColor',[0.749 0.749 0])
set(handles.powerAxes,'XTick',[])
set(handles.gamesAxes,'XTick',[])
set(handles.gamesAxes,'YTick',[])
drawnow
handles.power=y(it);
v=num2str(handles.power);
set(handles.powerValue,'String',v)
end
for it=1:Nt
iy=100-y(it);
bar(handles.powerAxes,1,iy);
axis(handles.powerAxes,[0 2 0 100])
set(handles.powerAxes,'XColor',[0.749 0.749 0])
set(handles.powerAxes,'YColor',[0.749 0.749 0])
set(handles.powerAxes,'XTick',[])
set(handles.gamesAxes,'XTick',[])
set(handles.gamesAxes,'YTick',[])
drawnow
handles.power=iy;
v=num2str(handles.power);
set(handles.powerValue,'String',v)
end
ix=ix-1;
end
----------------- venue.m
function venue(handles)
iVenue=get(handles.venueMenu,'Value');
switch iVenue
case 2
axes(handles.gamesAxes);
img=imread('joycebckgrnd.jpeg');
imagesc([0 27], [-2 17],img);
set(gca,'YDir','normal')
set(handles.gamesAxes,'XTick',[])
set(handles.gamesAxes,'YTick',[])
axis equal
hold on
case 3
axes(handles.gamesAxes);
img=imread('ruckerbckgrnd.jpeg');
imagesc([0 27], [-2 17],img);
set(gca,'YDir','normal')
set(handles.gamesAxes,'XTick',[])
set(handles.gamesAxes,'YTick',[])
axis equal
hold on
end
axes(handles.gamesAxes);
%% Draw net
netx=[22.25 23.15 23.65 24.15 25 24.6 24.25 23.65 23 22.65 22.25];
nety=[12 9.75 12 9.75 12 9.75 12 9.75 12 9.75 12];
line(netx,nety, 'Color', 'w')
%% Draw rim
rimx=[22.25 25.5];
rimy=[12 12];
line(rimx, rimy, 'Color', 'r', 'LineWidth', 4)
boardx=[25.5 25.5];
boardy= [11 16.75];
line(boardx,boardy, 'Color', 'w', 'LineWidth', 8)
%% Draw support
supportx=[25.75 27];
supporty=[15 17];
line(supportx, supporty, 'Color', 'w', 'LineWidth', 6)
axis([0 27 -2 17])
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
