Simple GUI calculator
I first created a simple GUI for the calculator
to perform +-*/.
When the user click the button the function arithmetic is called.
function calculator
figure('Position',[200 200 220 200],'Name','Calculator','NumberTitle','off','MenuBar','None','Resize','off');
txt=uicontrol('Style','Text','Position',[10 165 200 30],'String','0');
global stack value top op tops opstack num1 num2 flag;
flag=0;
x=10;
y=130;
top=1;
tops=1;
name=['7','8','9','/','4','5','6','*','1','2','3','-','0','+','C','='];%k
for k=1:size(name,2)
uicontrol('Style','Pushbutton','Position',[x y 50 30],'String' ,name(k),'Callback',@arithmetic) ;
x=x+50;
if(mod(k,4)==0)
x=10;y=y-35;
end
end
The String value of the pressed button is obtained using the get(object,’String’) function.
If the value is a number then it is placed in a stack.
If the value is a operator then the operator will be placed in the operator stack (opstack).
If the value is ‘C’ then the stacks top is set to 1.
function arithmetic(object,~)
num=str2double(get(object,'String'));
if((num>=0)&&(num<=9))
value=num;
evaluate();
else
op=get(object,'String');
if(op=='C')
top=1;
tops=1;
set(txt,'String','0');
else
operator();
end
end
end
The function evaluate is called to add the number to the stack.
If the stack (1) =0 and the stack (2) =9, then the stack (1) =9.
This is because, when the user enters zero and then 1, the stack value will be ‘01’. To obtain the correct value, it is better to remove the zero and display as ‘1’.
If the user enters numbers continuously without entering an operator then the values should be concatenated at each time.
For example, if the user enter 2, 4, 5 and then +, the values 245 should be displayed as a single number.
function evaluate()
stack(top)=value;
if((stack(1)==0)&&(top==2))
stack(1)=stack(top);
top=top-1;
end
str=num2str(stack(1));
for i=2:top
str=strcat(str,num2str(stack(i)));
end
top=top+1;
set(txt,'String',' ');
set(txt,'String',str);
flag=0;
end
If the user enter an operator then the operator will be added to the operator stack and the first number to perform the arithmetic operation is stored in the variable ‘num1’.
If the user enters an operator again then the operator will be added to the stack and the second number will be stored in the variable ‘num2’.
After getting the two numbers, the corresponding arithmetic operation is done based on the operator in the opstack(1).
The result will be stored in the variable ‘num1’.
function operator()
if((top~=1)||(flag==1))
opstack(tops)=op;
tops=tops+1;
if((tops==2)&&(flag~=1))
str=num2str(stack(1));
for i=2:top-1
str=strcat(str,num2str(stack(i)));
end
num1=str2double(str);
flag=1;
top=1;
elseif(tops>=3)
if(flag==0)
str=num2str(stack(1));
for i=2:top-1
str=strcat(str,num2str(stack(i)));
end
num2=str2double(str);
top=1;
if(opstack(tops-1)=='=')
calculate();
set(txt,'String',' ');
set(txt,'String',num2str(num1));
flag=1;
tops=1;
else
calculate();
set(txt,'String',' ');
set(txt,'String',num2str(num1));
flag=1;
tmp=opstack(tops-1);
opstack(1)=tmp;
tops=tops-1;
end
else
tmp=opstack(tops-1);
opstack(1)=tmp;
tops=tops-1;
end
end
end
end
function calculate()
switch (opstack(1))
case '+'
num1=num1+num2;
case '-'
num1=num1-num2;
case '/'
num1=num1/num2;
case '*'
num1=num1*num2;
end
end
end
Did u find the material useful?
9 comments:
hello this is rohit . I am getting some problem during my project.
I am working on face recognition project with image compression tech. and back propagation . I have created data set of 25 images with 8 by 8 dimension after data compression tech. unknown image has also same dimension. when I am giving input in backpropagation algo , it is not tacking as an input . showing wrong dimension of target input. please help me.
if you want my coding I can give you.
thank you
sorry I'm not good in MATLAB programming I copied your codes but I couldn't run it
gaves me error as '' function calculator
|
Error: Function definitions are not permitted in this context.''
any solution ?
@Asan Masraf
Can you mail me your error query to my mail. I can send you back the MATLAB code for the calculator.
I got these kind of error please rectify it ...
Error: File: arithmetic.m Line: 1 Column: 28
Unexpected MATLAB operator.
??? Error while evaluating uicontrol Callback
function arithmetic(object,~) error in this line .... why negation symbol is used whether its correct or something missing?
@Rajesh K
The error says that you have a '.' operator in line 1 and column 28 so try to remove it or
Try to copy the code again from the post.It seems that there is error in copying the code.
@Rajesh K
Negation is used to denote there is no other parameter is passed to the function.
There should not any error because of it.
the best bro!
Can you tell me what this code does,line by line?Please
Enjoyed Reading? Share Your Views