Using a While Loop with a Pushbutton in GUI
6 views (last 30 days)
Show older comments
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 Comments
Rik
on 8 May 2018
In that case, what is your homework assignment? Because I can't see a reason why you would put a loop here.
Answers (0)
See Also
Categories
Find more on Graphics Object Properties 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!