Using a While Loop with a Pushbutton in GUI

5 visualizzazioni (ultimi 30 giorni)
Logan Schwenker
Logan Schwenker il 8 Mag 2018
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 Commenti
Rik
Rik il 8 Mag 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
Logan Schwenker il 8 Mag 2018
Modificato: Walter Roberson il 8 Mag 2018
I agree with you, I figured something else out. I really do appreciate your help in case you're curious what I finished with here it is. Doesn't really make sense the way it is, but at least the project is finished.
function varargout = GUIV3(varargin)
% GUIV3 MATLAB code for GUIV3.fig
% GUIV3, by itself, creates a new GUIV3 or raises the existing
% singleton*.
%
% H = GUIV3 returns the handle to a new GUIV3 or the handle to
% the existing singleton*.
%
% GUIV3('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in GUIV3.M with the given input arguments.
%
% GUIV3('Property','Value',...) creates a new GUIV3 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before GUIV3_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to GUIV3_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help GUIV3
% Last Modified by GUIDE v2.5 08-May-2018 13:26:33
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @GUIV3_OpeningFcn, ...
'gui_OutputFcn', @GUIV3_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before GUIV3 is made visible.
function GUIV3_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to GUIV3 (see VARARGIN)
% Choose default command line output for GUIV3
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes GUIV3 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = GUIV3_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (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 edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (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
function edit2_Callback(hObject, eventdata, handles)
% hObject handle to edit2 (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 edit2 as text
% str2double(get(hObject,'String')) returns contents of edit2 as a double
% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit2 (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
function edit3_Callback(hObject, eventdata, handles)
% hObject handle to edit3 (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 edit3 as text
% str2double(get(hObject,'String')) returns contents of edit3 as a double
% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit3 (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
function edit4_Callback(hObject, eventdata, handles)
% hObject handle to edit4 (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 edit4 as text
% str2double(get(hObject,'String')) returns contents of edit4 as a double
% --- Executes during object creation, after setting all properties.
function edit4_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit4 (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 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;
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','markers',16)
title('Maximizing Horsepower with Set Cost')
xlabel('Cost ($)')
ylabel('Horsepower (ft-lbs/s)')
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
hold on
currentValue=str2double(get(handles.counter,'String'));
added=sprintf('%d',int32(currentValue +1));
set(handles.counter,'String',added);
p=1;
for k=1:str2double(added)
p=1+p;
end
set(handles.text11,'string',num2str(p))
% --- Executes on key press with focus on pushbutton1 and none of its controls.
function pushbutton1_KeyPressFcn(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata structure with the following fields (see MATLAB.UI.CONTROL.UICONTROL)
% Key: name of the key that was pressed, in lower case
% Character: character interpretation of the key(s) that was pressed
% Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed
% handles structure with handles and user data (see GUIDATA)

Accedi per commentare.

Risposte (0)

Categorie

Scopri di più su Graphics Object Properties in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by