Using a While Loop with a Pushbutton in GUI
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
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
on 8 May 2018
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
on 8 May 2018
Rik
on 8 May 2018
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
on 8 May 2018
Logan Schwenker
on 8 May 2018
Rik
on 8 May 2018
Then use either solution we provided. You don't need a repeat structure, because the user is the repeating structure.
Logan Schwenker
on 8 May 2018
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.
Logan Schwenker
on 8 May 2018
Edited: Walter Roberson
on 8 May 2018
Answers (0)
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!