Using a While Loop with a Pushbutton in GUI
이전 댓글 표시
Hello, I'm trying to limit the number of attempts a user of this graphical interface can calculate the cost and horsepower. I'd like to limit the attempts to 10 (I'm using 3 right now to assist in troubleshooting). I implemented a while loop and am trying to get the program to work so that each time horsepower and cost are calculated, the loop's count will increase by 1 eventually reaching the 3 mark where I'd like a message box to say 'max attempts' or something of that sort. The problem I'm getting right now is if I hit the calculate button once, the while loop instantly goes to 3 where I'd like it to index once each time the calculate button is pressed. Can somebody help me with this? Thanks in advance!!
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
f=get(handles.edit1,'string')
s=get(handles.edit2,'string')
d=get(handles.edit3,'string')
l=get(handles.edit4,'string')
cost=str2double(f)+str2double(s)/10+str2double(d)*20+str2double(l)*15
force=str2double(f)*str2double(d)/(60/str2double(s))
horsepower=force/6600
k=0;
while k<3
k=k+1;
if k==3
msgbox('max attempts')
else
if str2double(d)/2>=str2double(l)
msgbox('Connecting rod length must be longer than 1/2 the crankshaft diameter.')
set(handles.text6,'string','')
set(handles.text7,'string','')
set(handles.text8,'string','')
set(handles.text9,'string','')
else
if cost>=1000
msgbox('The budget of this crankshaft is $1000 or less, please enter smaller values.')
set(handles.text6,'string','')
set(handles.text7,'string','')
set(handles.text8,'string','')
set(handles.text9,'string','')
else
plot(cost,horsepower,'xr')
set(handles.text6,'string','The manufacturing cost of this crankshaft (in US dollars) is: ')
set(handles.text7,'string',num2str(cost))
set(handles.text8,'string','The horsepower of your design is: ')
set(handles.text9,'string',num2str(horsepower))
end
end
end
end
hold on
댓글 수: 10
Right know your button gets pushed and you enter the while loop and k increments inside this loop. You want to increment k outside your loop when your button gets pressed. I suggest you do not use a while loop for this (is doable but your gui would be stuck in this loop until k reaches 10). I suggest storing k in appdata/guidata and to use if to check what your callback needs to do.
function pushbutton1_Callback(hObject,~,handles)
k=getappdata(hObject,'k');
if isempty(k)
k=0
end
if k<=10
%code here
else
msgbox('max attempts')
end
k=k+1;
setappdata(hObject,'k',k)
end
Rik
2018년 5월 8일
Or even better: update the value before you start the (apparently expensive) calculation.
function pushbutton1_Callback(hObject,~,handles)
k=getappdata(hObject,'k');
if isempty(k)
k=0
end
k=k+1;
setappdata(hObject,'k',k)
if k<=10
%code here
else
msgbox('max attempts')
end
end
Logan Schwenker
2018년 5월 8일
Rik
2018년 5월 8일
Do you want to limit the number of times the user can hit the button and have the calculation run? Or do you want the user to hit the button once and have the calculation run 10 times?
Logan Schwenker
2018년 5월 8일
Logan Schwenker
2018년 5월 8일
Rik
2018년 5월 8일
Then use either solution we provided. You don't need a repeat structure, because the user is the repeating structure.
Logan Schwenker
2018년 5월 8일
Rik
2018년 5월 8일
In that case, what is your homework assignment? Because I can't see a reason why you would put a loop here.
Logan Schwenker
2018년 5월 8일
편집: Walter Roberson
2018년 5월 8일
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Graphics Object Properties에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!