how can i assign few different calculation for a single pusshbutton

hye. i have to design a program to calculate the Op-amp circuits's Voltage gain.here is a brief idea of how this program should be. when the user initiates the program, there will be a popupmenu (with three different choices or types of circuits), three edit_text inputs and a calculate_pushbutton. user selects the type of circuit in the popupmenu and keys in the 3 different values example like A B C. hw can i program the code below to have it calculate the values using different formulas with respect to the circuit type. example like if the user selects circuit one and keys in the value the calculate_pushbutton should calculate using the formula A/B. and if user selects circuit 2, A*C. i have done the program to get data from user in the edit_Text. and also a reset button. but now the problem is how can i assign three different formulas in the calculate_pushbutton. below is the codings i made to calculate. but it doesnt work.
switch get(handles.popupmenu,'Value')
case 1
A = get(input1_editText,'String');
B = get(input2_editText,'String');
C = get(input3_editText,'String'));
total = str2num(A)/str2num(B);
answer=num2str(total);
set(handles.answer_staticText,'String',answer;
case 2
A = get(input1_editText,'String');
B = get(input2_editText,'String');
C = get(input3_editText,'String'));
total = str2num(A)*str2num(C);
answer=num2str(total);
set(handles.answer_staticText,'String',answer;
set
otherwise
end
thank you for your help

 Accepted Answer

In the callback function of the Push Button, you can use val = get(handles.popupmenu,'Value') and then use an if else statement like:
if val==1
A = get(input1_editText,'String');
B = get(input2_editText,'String');
total = str2num(A)/str2num(B);
elseif val==2
A = get(input1_editText,'String');
C = get(input3_editText,'String');
total = str2num(A)*str2num(C);
and so on.. Hope this helps!

2 Comments

i tried the codes that u have mentioned. this is how my code for the popupmenu looks like:
switch get(handles.popupmenu1,'Value')
case 1
set(handles.calculate_pushbutton,'Value');
case 2
set(handles.calculate_pushbutton,'Value');
case 3
set(handles.calculate_pushbutton,'Value');
otherwise
end
and this is the code for the calculate pushbutton. (i edited the previous inputs with the original one). i still cun run it.
val = get(handles.popupmenu,'Value')
if val==1
Ve = get(input1_editText,'String');
Vc = get(input2_editText,'String');
Vb = get(input3_editText,'String');
total = str2num(Vc)/str2num(Vb);
answer=num2str(total);
elseif val==2
Ve = get(input1_editText,'String');
Vc = get(input2_editText,'String');
Vb = get(input3_editText,'String');
total = str2num(Ve)/str2num(Vb);
answer=num2str(total);
else val==3
Ve = get(input1_editText,'String');
Vc = get(input2_editText,'String');
Vb = get(input3_editText,'String');
total = str2num(Ve)/str2num(Vc);
answer=num2str(total);
thank u for your help..
plus how can i show the answer? i have one statictext output.. answer_staticText

Sign in to comment.

More Answers (1)

Follow this link to use pop up menus the right way.
It should work.
Also, if you want to dispay data in static text use:
set(handles.statictext,'String',answer);
Hope this helps.

9 Comments

You can put the answer using the 'tag' of your static text which is answer_staticText so it will be set(handles.answer_staticText, 'String', answer);
thank u for ur help. i really appreciate it. by i cant seem to press the calculate button. i mean it doesnt work. i keep getting error. the calculation code i have programmed is
% --- Executes on button press in calculate_pushbutton.
function calculate_pushbutton_Callback(hObject, eventdata, handles)
% hObject handle to calculate_pushbutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
val = get(handles.popupmenu,'Value');
if val==1
Ve = get(input1_editText,'String');
Vc = get(input2_editText,'String');
Vb = get(input3_editText,'String');
total = str2num(Vc)/str2num(Vb);
answer=num2str(total);
set(handles.answer_staticText,'String',answer);
elseif val==2
Ve = get(input1_editText,'String');
Vc = get(input2_editText,'String');
Vb = get(input3_editText,'String');
total = str2num(Ve)/str2num(Vb);
answer=num2str(total);
set(handles.answer_staticText,'String',answer);
else val=3;
Ve = get(input1_editText,'String');
Vc = get(input2_editText,'String');
Vb = get(input3_editText,'String');
total = str2num(Ve)/str2num(Vc);
answer=num2str(total);
set(handles.answer_staticText,'String',answer);
end
what error are you getting when you press the button?
there is a "ding" sound when i hit the calculate button. and the matlab shows these errors..
??? Reference to non-existent field 'popupmenu'.
Error in ==> VoltGain>calculate_pushbutton_Callback at 159 val = get(handles.popupmenu,'Value');
Error in ==> gui_mainfcn at 96 feval(varargin{:});
Error in ==> VoltGain at 42 gui_mainfcn(gui_State, varargin{:});
Error in ==> @(hObject,eventdata)VoltGain('calculate_pushbutton_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback
You should check the 'Tag' of your pop up menu by going to its property inspector. If that is not named 'popmenu' then you can change it to that or change in your code according to the 'Tag'. It should be working fine then.
yea i checked n there was a small mistake. i already rectify it. now im getting a new set of errors.
??? Undefined function or variable 'input1_editText'.
Error in ==> VoltGain>calculate_pushbutton_Callback at 161 Ve = get(input1_editText,'String');
Error in ==> gui_mainfcn at 96 feval(varargin{:});
Error in ==> VoltGain at 42 gui_mainfcn(gui_State, varargin{:});
Error in ==> @(hObject,eventdata)VoltGain('calculate_pushbutton_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback
You must use handles.input1_editText and so on to get the string values. Like: get(handles.input1_editText,'String')
owh my god. u sir are a genius.. it works. it finally works.. thank you so so much sir..

Sign in to comment.

Categories

Asked:

on 19 Jul 2013

Community Treasure Hunt

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

Start Hunting!