Lets Learn together... Happy Reading

" Two roads diverged in a wood, and I,
I took the one less traveled by,
And that has made all the difference "-Robert Frost

Simple GUI Calculator in MATLAB

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.


Additional Information: To perform all the complex calculations use Texas Instruments.








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?




like button Like "IMAGE PROCESSING" page

Excel file to matlab


EXCEL2UITABLE:

Method: 1
The data from excel file can be imported to matlab using xlsread function.

[numericdata,stringdata]=xlsread(filename, sheet name);

 The numeric data in the sheet will be stored in the first variable ‘numericdata’.

The strings or characters in the sheet will be stored in the second variable ‘stringdata’.

Method : 2
  Using uiimport , the user can interactively select the rows and data.
Syntax: uiimport('Employee.xls')

Sample program:
Here, a excel file by name ‘Employee.xls’ with sheet name ‘Employee Details’ contains some employee details. The details are imported from excel to matlab.

To obtain the heading of each column:
%[data heading]=xlsread('Employee.xls','Employee Details','A1:B1:C1');


[ID Text]=xlsread('Employee.xls','Employee Details');

The numeric and string data are stored in the ID and Text respectively.



To display the sheet in a table format:
The employee id,employee name and employee date of join is stored.
To calculate the experience of each employee, the date of join obtained is converted to datenum and it is subtracted from the current date.
num=floor(now)-datenum(Text(i,3));
The number of years is obtained by using the function datestr(num,11);
Here the value 11 will return the years in ‘YY’ format.

f = figure('Position',[400 400 400 150],'Name','Employee Details','NumberTitle','off');
dat=cell(size(Text,1),size(Text,2)+1);
for i=2:size(Text,1)
    num=floor(now)-datenum(Text(i,3));
    exp=datestr(num,11);
   
    dat(i-1,1)={ID(i-1,1)};
    dat(i-1,2)=Text(i,2);
    dat(i-1,3)=Text(i,3);
    dat(i-1,4)={exp};
   
end


columnname={'Employee ID','Employee Name','Date Of Join','Experience'};

columnformat = {'numeric', 'char', 'char', 'numeric'};

uitable('Units','normalized','Position',...
            [0.1,0.1,.9,.9], 'Data', dat,...
            'ColumnName', columnname,...
            'ColumnFormat', columnformat,'RowName',[]);



like button Like "IMAGE PROCESSING" page

Converting RGB image to Binary Image without using im2bw function

         In the first example, image is filled with primary colors (RGB). So I am finding the sum of the values in the pixel position. If the sum is greater than zero then the value will be 1(white) otherwise zero (black).

       In the second example, the following steps are needed to convert a RGB image to binary image.

  1. Convert the RGB image into grayscale image.
  2. Find the threshold value. If the value at the pixel position is greater than the threshold value then the value will be 1(white) else zero (black).

    function mybinary
    global GIm T1;
    A=imread('shapes.bmp');
    figure,imshow(A);
    title('Original image');
    B=zeros(size(A,1),size(A,2));
    for l=1:size(A,1)
        for m=1:size(A,2)
            if(sum(A(l,m,:))>0)
                B(l,m)=1;
            end
        end
    end
    B=logical(B);
    figure,imshow(B);



    Im=imread('gantrycrane.png');
    figure,imshow(Im);
    title('Original Image');
    %0.2989 * R + 0.5870 * G + 0.1140 * B
    GIm=uint8(zeros(size(Im,1),size(Im,2)));
    for m=1:size(Im,1)
        for n=1:size(Im,2)
            GIm(m,n)=0.2989*Im(m,n,1)+0.5870*Im(m,n,2)+0.1140*Im(m,n,3);
        end
    end
    we can perform the grayscale conversion without using the for loop:

    %GIm=0.2989*Im(:,:,1)+0.5870*Im(:,:,2)+0.1140*Im(:,:,3);



    
    
    ssz = get(0,'ScreenSize');
    T.fg=figure('Visible','on','Name','IMAGE THRESHOLDING','NumberTitle','off','Position', ssz);
    T.holder=axes('units','pixels','Position',[ssz(3)/35 ssz(4)/4 ssz(3)-(ssz(3)/3) ssz(4)-(ssz(4)/3)]);
    imshow(GIm);
    set(T.holder,'xtick',[],'ytick',[])
    T.slid=uicontrol('Style','Slider','Visible','on','Value',1,'Max',255,'Min',0,'Sliderstep',[1 1],'Position',[ssz(3)/35 ssz(4)/5 ssz(3)-(ssz(3)/3) 20],'Callback', @tresher);
    T.ent=uicontrol('Style','pushbutton','Visible','on','String','THRESHOLD VALUE','Position',[ssz(3)-(ssz(3)/4) ssz(4)-(ssz(4)/8) 105 30]);
    T.ed=uicontrol('Style','edit','Visible','on','String','0','Value',1,'Position',[ssz(3)-(ssz(3)/4) ssz(4)-(ssz(4)/6) 90 20]);
          function tresher(object,~)
            val=get(object,'value');
            in=GIm;
            T1=Imthreshold1(in,val);
            T.view1=imshow(T1);
            set(T.holder,'xtick',[],'ytick',[])
           
            set(T.ed,'String',val);
          end
                                                                                 
                                                                                 
                                                                                 
                                                                                 
                                                                                 
                                                                                 
      function Im=Imthreshold1(Image,Tvalue)
    sz=size(Image);
    mybin=zeros(size(Image));
    for i=1:sz(1)
        for j=1:sz(2)
            if(Image(i,j)>Tvalue)
                mybin(i,j)=1;
               
            end
        end
    end


    Instead of this for loop, the equivalent one line code is:

    %mybin(find(Image>Tvalue))=1;

    Explanation:
    The output of find(Image>Tvalue) will be the values that are greater than Tvalue.



    For instance,
    consider a matrix,

    >> A=[1,2,3,4;2,4,6,8;3,6,9,12];
    >> A

    A =

         1     2     3     4
         2     4     6     8
         3     6     9    12

    >> find(mod(A,2)==0)

    ans =

         2
         4
         5
         6
         8
        10
        11
        12




    
    
    Im=logical(mybin);
    end
    end



like button Like "IMAGE PROCESSING" page
Previous Post Next Post Home
Google ping Hypersmash.com